redis安装,redis项目以来,redis和spring整合,redis的service,redis的service实现类
一.redis安裝:
| Redis-3.2.6.tar.gz安裝,參考方式: (這里也可以用于安裝redis-4.0.9.tar.gz :? ? ?wget http://download.redis.io/releases/redis-4.0.9.tar.gz) 用源碼工程來編譯安裝 1、? 到官網(wǎng)下載最新stable版,這里使用的是:redis-3.2.6.tar.gz 2、? cd /usr/local ? 3、? mkdir?redis-src 4、? tar -zxvf ? ?redis-3.2.6.tar.gz? -C? ./redis-src/ 2、解壓源碼并進(jìn)入目錄cd ?/usr/local/redis-src/redis-3.2.6 3、?先執(zhí)行make,檢查是否報(bào)錯 如果報(bào)錯提示缺少gcc,則安裝gcc :? yum install -y gcc 如果報(bào)錯提示:Newer version ofjemalloc required 則在make時加參數(shù):make MALLOC=libc?(如果沒有報(bào)錯,直接使用make命令) ? 4、安裝redis,指定安裝目錄,如 /usr/local/redis make PREFIX=/usr/local/redis install ? 5、拷貝一份配置文件到安裝目錄下 切換到源碼目錄,里面有一份配置文件redis.conf,然后將其拷貝到安裝路徑下 cp redis.conf /usr/local/redis/ ? 6、啟動redis cd /usr/local/redis bin/redis-server redis.conf ? (如果想后臺進(jìn)程運(yùn)行,修改:daemonize yes) ? vim redis.conf 將下面的變量修改為: daemonize yes ? 將bind的id換成真實(shí)的ip地址,比如: bind 192.168.1.1 ? 在集群配置中,要對redis配置密碼,修改的配置是將redis.conf這個文件的下面的變量配置成如下的: requirepass cloudTplWebapp??? (這里設(shè)置一個密碼:cloudTplWebapp) 7、連接redis 另開一個xshell,然后: #cd /usr/local/redis/ [root@Hadoop?redis]# bin/redis-cli? 127.0.0.1:6379> ? 設(shè)置timeout中的值為120 bin/redis-server redis.conf bin/redis-cli -h 192.168.106.103 client list 查看客戶端連接情況
|
?
二.配置Maven依賴
| <!-- 整合redis所需的jar包 --> ??????? <dependency> ????? ??? <groupId>org.springframework.data</groupId> ????? ??? <artifactId>spring-data-redis</artifactId> ????? ??? <version>1.6.0.RELEASE</version> ????? </dependency> ????? <dependency> ????? ??? <groupId>redis.clients</groupId> ????? ??? <artifactId>jedis</artifactId> ????? ??? <version>2.7.3</version> ????? </dependency> |
二.配置applicationContext.xml
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ?? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ?? xmlns:p="http://www.springframework.org/schema/p" ?? xmlns:context="http://www.springframework.org/schema/context" ?? xmlns:tx="http://www.springframework.org/schema/tx" ?? xmlns:task="http://www.springframework.org/schema/task" ?? xsi:schemaLocation="? ??? http://www.springframework.org/schema/beans?? ??? http://www.springframework.org/schema/beans/spring-beans-3.1.xsd? ??? http://www.springframework.org/schema/tx?? ??? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ??? http://www.springframework.org/schema/mvc ??? http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ??? http://www.springframework.org/schema/cache ??? http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ??? http://www.springframework.org/schema/task ??? http://www.springframework.org/schema/task/spring-task-3.1.xsd ??? http://www.springframework.org/schema/context?? ??? http://www.springframework.org/schema/context/spring-context-3.0.xsd ??? http://www.springframework.org/schema/aop ??? http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> ??? ??? ?? <!-- 引入jdbc配置文件 --> ?? <context:property-placeholder location="classpath:jdbc.properties,classpath:redis.properties" /> ??? ?? <!—在自己的application中加入如下配置 --> ?? <import resource="redis-context.xml"/> </beans> |
?其中redis-context.xml的內(nèi)容如下:
?
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><beans><!-- Jedis線程 --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${redis.pool.maxTotal}" /><property name="maxIdle" value="${redis.pool.maxIdle}" /><property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /></bean><!-- 配置多個redis的方式,start --><bean id="jedis.shardInfo.0" class="redis.clients.jedis.JedisShardInfo"><constructor-arg index="0" value="${redis.ip.0}" /><constructor-arg index="1" value="${redis.port.0}" /></bean><!--<bean id="jedis.shardInfo.1" class="redis.clients.jedis.JedisShardInfo"><constructor-arg index="0" value="${redis.ip.1}" /><constructor-arg index="1" value="${redis.port.1}" /></bean>--><bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"><constructor-arg index="0" ref="jedisPoolConfig" /><constructor-arg index="1"><list><ref bean="jedis.shardInfo.0" /><!--<ref bean="jedis.shardInfo.1" />--></list></constructor-arg></bean><!-- 配置多個redis的方式,end --><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><!-- 以下是配置多個redis的方式 :start --><!--<property name="shardInfo" value="shardedJedisPool"></property>--><!-- 以上是配置多個reids的方式 :end --><!-- 以下是配置單個redis的方式:start --><property name="hostName" value="${redis.ip.0}" /><property name="port" value="${redis.port.0}" /><property name="password" value="${redis.pass}"/><property name="timeout" value="${redis.timeout}"/><property name="database" value="${redis.default.db}"/><property name="poolConfig" ref="jedisPoolConfig" /><!-- 以上是配置單個redis的方式:end--></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /></bean><bean id="redisService" class="xx.tpl.rediscache.impl.RedisServiceImpl"><property name="redisTemplate" ref="redisTemplate" /><property name="expirationtime" value="${redis.expiration}"/></bean> </beans>?
?
?
?
?
編寫redis.properties屬性文件
| #redis.pool.maxActive=1024 #redis.pool.maxIdle=200 #redis.pool.maxWait=1000 #redis.pool.testOnBorrow=true ? redis.ip.0=192.168.1.1 redis.port.0=6379 #密碼 redis.pass=cloudTplWebapp #設(shè)置默認(rèn)的連接的db redis.default.db=0 redis.timeout=100000 redis.maxActive=300 ? redis.ip.1=192.168.1.1 redis.port.1=6379 redis.pool.maxTotal=1024 redis.pool.maxIdle=200 redis.pool.maxWaitMillis=1000 redis.pool.testOnBorrow=true redis.expiration=300 |
?
Redis的RedisService如下:
| package xxx.xxx.xxx.rediscache; ? import java.io.Serializable; import java.util.Map; import java.util.Set; ? /** ?* RedisService.java redis的服務(wù)操作類 ?* @attention ?* @author toto ?* @date 2017-1-12 ?* @note begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ?*/ public interface RedisService<T> { ??? ???????? /** ???? * 將內(nèi)容保存到緩存中 ???? * ???? * @param key ???? * @param value ???? * @throws Exception ???? * @attention ???? * @author toto ???? * @date 2017-1-12 ???? * @note? begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ???? */ ???????? public void save(final String key, Object value) throws Exception; ? ???????? /** ???????? ?* 獲取內(nèi)存中的內(nèi)容 ???????? ?* @param <T> ???????? ?* @param key ???????? ?* @param elementType ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 原始創(chuàng)建 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public <T> T getByKey(final String key, Class<T> elementType) throws Exception; ???????? ???????? /** ???????? ?* 通過傳遞key刪除所有的在緩存中的內(nèi)容 ???????? ?* @param key ???????? ?* @attention ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public void del(String... key) throws Exception; ???????? ???????? /** ???????? ?* 批量刪除<br/> ???????? ?* @param pattern ???????? ?* @throws Exception ???????? ?* @attention 該操作會執(zhí)行模糊查詢,請盡量不要使用,以免影響性能或誤刪 ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note begin modify by 涂作權(quán)? 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public void batchDel(String... pattern) throws Exception; ? ???????? /** ???????? ?* 取得緩存(int型) ???????? ?* @param key ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention 方法的使用注意事項(xiàng) ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ???????? ?*/ ???????? public Integer getInt(String key) throws Exception; ???????? ???????? /** ???????? ?* 取得緩存(字符串類型)? ???????? ?* @param key ???????? ?* @return ???????? ?* @attention 方法的使用注意事項(xiàng) ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public String getStr(String key) throws Exception; ???????? ???????? /** ???????? ?* 取得緩存(字符串類型) ???????? ?* @param key ???????? ?* @param retain ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public String getStr(String key,boolean retain) throws Exception; ???????? ???????? /** ???????? ?* 獲取緩存<br> ???????? ?* @param key ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention 注:基本數(shù)據(jù)類型(Character除外),請直接使用get(String key, Class<T> clazz)取值 ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public Object getObj(String key) throws Exception; ???????? ???????? /** ???????? ?* 獲取緩存<br>? ???????? ?* @param key ???????? ?* @param retain 是否保留 ???????? ?* @return ???????? ?* @attention 注:java 8種基本類型的數(shù)據(jù)請直接使用get(String key, Class<T> clazz)取值 ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ???????? ?*/ ???????? public Object getObj(String key,boolean retain) throws Exception ; ???????? ???????? /** ???????? ?* 獲取緩存 ???????? ?* @param <T> ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention 方法的使用注意事項(xiàng) ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 修改人 修改時間?? 修改內(nèi)容摘要說明 ???????? ?*/ ???????? public <T> T get(String key, Class<T> clazz) throws Exception; ???????? ???????? /** ???? * 將value對象寫入緩存 ???? * @param key ???? * @param value ???? * @param time 失效時間(秒) ???? */ ??? public void set(String key,Object value,long expirationtime)throws Exception; ??? ??? /** ???? * 遞減操作 ???? * @param key ???? * @param by ???? * @return ???? */ ??? public double decr(String key, double by)throws Exception; ??? ??? /** ???? * 遞增操作 ???? * @param key ???? * @param by ???? * @return ???? */ ??? public double incr(String key, double by)throws Exception; ??? ??? /** ???? * 獲取double類型值 ???? * @param key ???? * @return ???? */ ??? public double getDouble(String key) throws Exception; ??? ??? /** ???? * 設(shè)置double類型值 ???? * @param key ???? * @param value ???? * @param time 失效時間(秒) ???? */ ??? public void setDouble(String key, double value,long expirationtime) throws Exception; ??? ??? /** ???? * 設(shè)置double類型值 ???? * @param key ???? * @param value ?? ??* @param time 失效時間(秒) ???? */ ??? public void setInt(String key, int value, long expirationtime) throws Exception; ??? ??? /** ???? * 將map寫入緩存 ???? * @param key ???? * @param map ???? * @param time 失效時間(秒) ???? */ ??? public <T> void setMap(String key, Map<String, T> map, long expirationtime)throws Exception; ??? ??? /** ???? * 向key對應(yīng)的map中添加緩存對象 ???? * @param key ???? * @param map ???? */ ??? public <T> void addMap(String key, Map<String, T> map)throws Exception; ??? ??? /** ???? * 向key對應(yīng)的map中添加緩存對象 ???? * @param key?? cache對象key ???? * @param field map對應(yīng)的key ???? * @param value???? 值 ???? */? ??? public void addMap(String key, String field, String value)throws Exception; ??? ??? /** ???? * 向key對應(yīng)的map中添加緩存對象 ???? * @param key?? cache對象key ???? * @param field map對應(yīng)的key ???? * @param obj?? 對象 ???? */? ??? public <T> void addMap(String key, String field, T obj)throws Exception; ??? ??? /** ???? * 獲取map緩存 ???? * @param key ???? * @param clazz ???? * @return ???? */? ??? public <T> Map<String, T> mget(String key, Class<T> clazz)throws Exception; ??? ??? /** ???? * 獲取map緩存中的某個對象 ???? * @param key ???? * @param field ???? * @param clazz ???? * @return ???? */ ??? public <T> T getMapField(String key, String field, Class<T> clazz)throws Exception; ??? ??? /** ???? * 刪除map中的某個對象 ???? * @author lh ???? * @date 2016年8月10日 ???? * @param key?? map對應(yīng)的key ???? * @param field map中該對象的key ???? */? ??? public void delMapField(String key, String... field)throws Exception; ??? ??? /** ???? * 指定緩存的失效時間 ???? * ???? * @author FangJun ???? * @date 2016年8月14日 ???? * @param key 緩存KEY ???? * @param time 失效時間(秒) ???? */ ??? public void expire(String key, long expirationtime) throws Exception; ??? ??? /** ???? * 添加set ???? * @param key ???? * @param value ???? */ ??? public void sadd(String key, String... value) throws Exception; ??? ??? /** ???? * 刪除set集合中的對象 ???? * @param key ???? * @param value ???? */? ??? public void srem(String key, String... value) throws Exception; ??? ??? /** ???? * set重命名 ???? * @param oldkey ???? * @param newkey ???? */ ??? public void srename(String oldkey, String newkey)throws Exception; ??? ??? /** ???? * 模糊查詢keys ???? * @param pattern ???? * @return ???? */ ??? public Set<Serializable> keys(String pattern)throws Exception; } |
?
Redis的RedisServiceImpl的實(shí)現(xiàn)類:
| package xxx.xxx.xxx.rediscache.impl; ? import java.io.Serializable; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; ? import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; ? import com.ucap.tpl.rediscache.RedisService; import com.ucap.tpl.utils.SerializeUtils; ? @SuppressWarnings({"unchecked"}) public class RedisServiceImpl<T> implements RedisService<T> { ???????? @SuppressWarnings("unused") ???????? private static Log logger = LogFactory.getLog(RedisServiceImpl.class); ???????? private static RedisTemplate<Serializable, Serializable> redisTemplate; ???????? private long expirationtime; ???????? ???????? public long getExpirationtime() { ?????????????????? return expirationtime; ???????? } ? ???????? public RedisTemplate<Serializable, Serializable> getRedisTemplate() { ?????????????????? return redisTemplate; ???????? } ? ???????? @SuppressWarnings("static-access") ???????? public void setRedisTemplate( ??????????????????????????? RedisTemplate<Serializable, Serializable> redisTemplate) { ?????????????????? this.redisTemplate = redisTemplate; ???????? } ? ???????? public void setExpirationtime(long expirationtime) { ?????????????????? this.expirationtime = expirationtime; ???????? } ???????? ???????? /** ???????? ?* 保存內(nèi)容到緩存中 ???????? ?*/ ???????? public void save(final String key, Object value) throws Exception { ?????????????????? final byte[] vbytes = SerializeUtils.serialize(value); ?????????????????? redisTemplate.execute(new RedisCallback<Object>() { ? ??????????????????????????? public Object doInRedis(RedisConnection connection) ?????????????????????????????????????????????? throws DataAccessException { ???????????????????????????????????? connection.set(redisTemplate.getStringSerializer().serialize(key), vbytes); ???????????????????????????????????? return null; ??????????????????????????? } ?????????????????? }); ???????? } ???????? ???????? /** ???????? ?* 通過key取到保存在緩存中的內(nèi)容 ???????? ?*/ ???????? public <T> T getByKey(final String key, Class<T> elementType) throws Exception { ?????????????????? try { ??????????????????????????? return redisTemplate.execute(new RedisCallback<T>() { ???????????????????????????????????? public T doInRedis(RedisConnection connection) ??????????????????????????????????????????????????????? throws DataAccessException { ?????????????????????????????????????????????? byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key); ?????????????????????????????????????????????? if (connection.exists(keyBytes)) { ??????????????????????????????????????????????????????? byte[] valueBytes = connection.get(keyBytes); ??????????????????????????????????????????????????????? ??????????????????????????????????????????????????????? T value = (T) SerializeUtils.unserialize(valueBytes); ??????????????????????????????????????????????????????? return value; ?????????????????????????????????????????????? } ?????????????????????????????????????????????? return null; ???????????????????????????????????? } ??????????????????????????? }); ?????????????????? } catch (Exception e) { ??????????????????????????? e.printStackTrace(); ?????????????????? } ?????????????????? return null; ???????? } ???????? ???????? /** ???????? ?* 通過傳遞key刪除所有的在緩存中的內(nèi)容 ???????? ?* @param key ???????? ?* @attention 這里個的key是可變參數(shù)的key ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public void del(String... key) throws Exception { ?????????????????? if (key != null && key.length > 0) { ??????????????????????????? if (key.length == 1) { ???????????????????????????????????? redisTemplate.delete(key[0]); ??????????????????????????? } else { ???????????????????????????????????? redisTemplate.delete(CollectionUtils.arrayToList(key)); ??????????????????????????? } ?????????????????? } ???????? } ???????? ???????? /** ???????? ?* 批量刪除<br/> ???????? ?* @param pattern ???????? ?* @throws Exception ???????? ?* @attention 該操作會執(zhí)行模糊查詢,請盡量不要使用,以免影響性能或誤刪 ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note begin modify by 涂作權(quán)? 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public void batchDel(String... pattern) throws Exception { ?????????????????? for (String pt : pattern) { ??????????????????????????? redisTemplate.delete(redisTemplate.keys(pt + "*")); ?????????????????? } ???????? } ? ???????? /** ???????? ?* 取得緩存(int型) ???????? ?* @param key ???????? ?* @return ???????? ?* @throws Exception ???????? ?* @attention 方法的使用注意事項(xiàng) ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ???????? ?*/ ???????? public Integer getInt(String key) throws Exception { ?????????????????? String value = (String) redisTemplate.boundValueOps(key).get(); ?????????????????? if (StringUtils.isNotBlank(value)) { ??????????????????????????? return Integer.valueOf(value); ?????????????????? } ?????????????????? return null; ???????? } ? ???????? /** ???????? ?* 取得緩存(字符串類型)? ???????? ?* @param key ???????? ?* @return ???????? ?* @attention 方法的使用注意事項(xiàng) ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public String getStr(String key) throws Exception { ?????????????????? return (String) redisTemplate.boundValueOps(key).get(); ???????? } ? ???????? /** ???????? ?* 取得緩存(字符串類型) ???????? ?* @param key ???????? ?* @param retain ???????? ?* @return ???????? ?* @attention ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note? begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建 ???????? ?*/ ???????? public String getStr(String key,boolean retain) throws Exception { ?????????????????? String value = (String) redisTemplate.boundValueOps(key).get(); ?????????????????? if (!retain) { ??????????????????????????? redisTemplate.delete(key); ?????????????????? } ?????????????????? return value; ???????? } ? ???????? /** ???????? ?* 獲取緩存<br> ???????? ?* 注:基本數(shù)據(jù)類型(Character除外),請直接使用get(String key, Class<T> clazz)取值 ???????? ?* @param key ???????? ?* @return ???????? ?*/ ???????? public Object getObj(String key) throws Exception { ?????????????????? return redisTemplate.boundValueOps(key).get(); ???????? } ? ???????? /** ???????? ?* 獲取緩存<br>? ???????? ?* @param key ???????? ?* @param retain 是否保留 ???????? ?* @return ???????? ?* @attention 注:java 8種基本類型的數(shù)據(jù)請直接使用get(String key, Class<T> clazz)取值 ???????? ?* @author toto ???????? ?* @date 2017-1-12 ???????? ?* @note begin modify by 涂作權(quán) 2017-1-12? 原始創(chuàng)建 ???????? ?*/ ???????? public Object getObj(String key, boolean retain) throws Exception { ?????????????????? Object obj = redisTemplate.boundValueOps(key).get(); ?????????????????? if (!retain) { ??????????????????????????? redisTemplate.delete(key); ?????????????????? } ?????????????????? return obj; ???????? } ???????? ???????? /** ???? * 獲取緩存<br> ???? * 注:該方法暫不支持Character數(shù)據(jù)類型 ???? * @param key?? key ???? * @param clazz 類型 ???? * @return ???? */ ???????? public <T> T get(String key, Class<T> clazz) throws Exception { ??????? return (T) redisTemplate.boundValueOps(key).get(); ??? } ????? ??? /** ???? * 將value對象寫入緩存 ???? * @param key ???? * @param value ???? * @param time 失效時間(秒) ???? */? ??? public void set(String key,Object value,long expirationtime)throws Exception {? ??????? if(value.getClass().equals(String.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Integer.class)){ ???? ???????redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Double.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Float.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Short.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Long.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else if(value.getClass().equals(Boolean.class)){ ??????????? redisTemplate.opsForValue().set(key, value.toString()); ??????? }else{ ??????????? redisTemplate.opsForValue().set(key, SerializeUtils.serialize(value)); ??????? } ??????? if(expirationtime > 0){ ??????????? redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS); ??????? } ??? } ??? ??? /** ???? * 遞減操作 ???? * @param key ???? * @param by ???? * @return ???? */ ??? public double decr(String key, double by)throws Exception { ??????? return redisTemplate.opsForValue().increment(key, -by); ??? } ????? ??? /** ???? * 遞增操作 ???? * @param key ???? * @param by ???? * @return ?? ??*/ ??? public double incr(String key, double by)throws Exception { ??????? return redisTemplate.opsForValue().increment(key, by); ??? } ????? ??? /** ???? * 獲取double類型值 ???? * @param key ???? * @return ???? */ ??? public double getDouble(String key) throws Exception { ??????? String value = (String) redisTemplate.boundValueOps(key).get(); ??????? if(StringUtils.isNotBlank(value)){ ??????????? return Double.valueOf(value); ??????? } ??????? return 0d; ??? } ??? ??? /** ???? * 設(shè)置double類型值 ???? * @param key ???? * @param value ???? * @param time 失效時間(秒) ???? */ ??? public void setDouble(String key, double value,long expirationtime) throws Exception { ??????? redisTemplate.opsForValue().set(key, String.valueOf(value)); ??????? if(expirationtime > 0){ ??????????? redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS); ??????? } ??? } ??? ??? /** ???? * 設(shè)置double類型值 ???? * @param key ???? * @param value ???? * @param time 失效時間(秒) ???? */ ??? public void setInt(String key, int value, long expirationtime) throws Exception { ??????? redisTemplate.opsForValue().set(key, String.valueOf(value)); ??????? if(expirationtime > 0){ ??????????? redisTemplate.expire(key,expirationtime, TimeUnit.SECONDS); ??????? } ??? } ????? ??? /** ???? * 將map寫入緩存 ???? * @param key ???? * @param map ???? * @param time 失效時間(秒) ???? */ ??? public <T> void setMap(String key, Map<String, T> map, long expirationtime)throws Exception { ??????? redisTemplate.opsForHash().putAll(key, map); ??? } ??? ??? /** ???? * 向key對應(yīng)的map中添加緩存對象 ???? * @param key ???? * @param map ???? */ ??? public <T> void addMap(String key, Map<String, T> map)throws Exception { ??????? redisTemplate.opsForHash().putAll(key, map); ??? } ??? ??? /** ? ???* 向key對應(yīng)的map中添加緩存對象 ???? * @param key?? cache對象key ???? * @param field map對應(yīng)的key ???? * @param value???? 值 ???? */ ??? public void addMap(String key, String field, String value)throws Exception { ??????? redisTemplate.opsForHash().put(key, field, value); ??? } ????? ??? /** ???? * 向key對應(yīng)的map中添加緩存對象 ???? * @param key?? cache對象key ???? * @param field map對應(yīng)的key ???? * @param obj?? 對象 ???? */ ??? public <T> void addMap(String key, String field, T obj)throws Exception { ??????? redisTemplate.opsForHash().put(key, field, obj); ??? } ??? ??? /** ???? * 獲取map緩存 ???? * @param key ???? * @param clazz ???? * @return ???? */ ??? public <T> Map<String, T> mget(String key, Class<T> clazz)throws Exception { ?? ?????BoundHashOperations<Serializable, String, T> boundHashOperations = redisTemplate.boundHashOps(key); ??????? return boundHashOperations.entries(); ??? }? ?? ??? /** ???? * 獲取map緩存中的某個對象 ???? * @param key ???? * @param field ???? * @param clazz ???? * @return ???? */ ??? public <T> T getMapField(String key, String field, Class<T> clazz)throws Exception { ??????? return (T)redisTemplate.boundHashOps(key).get(field); ??? }? ????? ??? /** ???? * 刪除map中的某個對象 ???? * @author lh ???? * @date 2016年8月10日 ???? * @param key?? map對應(yīng)的key ???? * @param field map中該對象的key ???? */ ??? public void delMapField(String key, String... field)throws Exception { ??????? BoundHashOperations<Serializable, String, Object> boundHashOperations = redisTemplate.boundHashOps(key); ??????? boundHashOperations.delete(field); ??? } ????? ??? /** ???? * 指定緩存的失效時間 ???? * ???? * @author FangJun ???? * @date 2016年8月14日 ???? * @param key 緩存KEY ???? * @param time 失效時間(秒) ???? */ ??? public void expire(String key, long expirationtime) throws Exception { ??????? if(expirationtime > 0){ ??????????? redisTemplate.expire(key, expirationtime, TimeUnit.SECONDS); ??????? } ??? } ??? ??? /** ???? * 添加set ???? * @param key ???? * @param value ???? */ ??? public void sadd(String key, String... value) throws Exception {? ??????? redisTemplate.boundSetOps(key).add(value); ??? } ??? ??? /** ???? * 刪除set集合中的對象 ???? * @param key ???? * @param value ???? */ ??? public void srem(String key, String... value) throws Exception { ??????? redisTemplate.boundSetOps(key).remove(value); ??? } ??? ??? /** ???? * set重命名 ???? * @param oldkey ???? * @param newkey ???? */ ??? public void srename(String oldkey, String newkey)throws Exception {? ??????? redisTemplate.boundSetOps(oldkey).rename(newkey); ??? } ????? ??? /** ???? * 模糊查詢keys ???? * @param pattern ???? * @return ???? */ ??? public Set<Serializable> keys(String pattern)throws Exception {? ??????? return redisTemplate.keys(pattern); ??? } } |
?依賴的SerializeUtils.java代碼如下:
?
package xx.tpl.utils;import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;/*** SerializeUtils.java 序列化相關(guān)的操作類* @attention* @author toto* @date 2017-1-12* @note begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建*/ public class SerializeUtils {/*** 將對象進(jìn)行序列化* @param object* @return* @attention 傳遞對象,將* @author toto* @date 2017-1-12 * @note begin modify by 修改人 修改時間 修改內(nèi)容摘要說明*/public static byte[] serialize(Object object) {ObjectOutputStream oos = null;ByteArrayOutputStream baos = null;try {// 序列化baos = new ByteArrayOutputStream();oos = new ObjectOutputStream(baos);oos.writeObject(object);byte[] bytes = baos.toByteArray();return bytes;} catch (Exception e) {throw new RuntimeException(e.getMessage(), e);}}/*** 反序列化* @param bytes* @return* @attention 反序列化的操作* @author toto* @date 2017-1-12 * @note begin modify by 涂作權(quán) 2017-1-12 原始創(chuàng)建*/public static Object unserialize(byte[] bytes) {ByteArrayInputStream bais = null;try {// 反序列化bais = new ByteArrayInputStream(bytes);ObjectInputStream ois = new ObjectInputStream(bais);return ois.readObject();} catch (Exception e) {throw new RuntimeException(e.getMessage(), e);}} }?
?
?
?
?
在BaseController 中引入如下的內(nèi)容:
| /** 獲取redis的service連接 */ @Resource(name = "redisService") protected RedisServiceImpl redisService; |
最后,在項(xiàng)目中就可以通過redisService調(diào)用其它的redis方法了。
?
?
總結(jié)
以上是生活随笔為你收集整理的redis安装,redis项目以来,redis和spring整合,redis的service,redis的service实现类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技嘉主板怎么装系统 技嘉主板安装系统详解
- 下一篇: XDocReport 的简单使用 操作w