spring集成redis

来源:互联网 发布:java jsoup 爬虫实例 编辑:程序博客网 时间:2024/06/14 06:04

一、引入sping对redis的封装包

<dependency>              <groupId>org.springframework.data</groupId>              <artifactId>spring-data-redis</artifactId>              <version>1.0.2.RELEASE</version>      </dependency><dependency>              <groupId>redis.clients</groupId>              <artifactId>jedis</artifactId>              <version>2.1.0</version>       </dependency><dependency>     <groupId>commons-pool</groupId>    <artifactId>commons-pool</artifactId>    <version>1.5.5</version>   </dependency>

二、redis相关bean注入

在spring-redis.xml中:
<!-- redis配置文件 -->  <context:property-placeholder location="classpath:conf/redis-config.properties" ignore-unresolvable="true" />  <!-- redis连接池 -->  <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">   <property name="maxActive" value="${redis_max_active}"></property>   <property name="maxIdle" value="${redis_max_idle}"></property>  <property name="maxWait" value="${redis_max_wait}"></property>  <property name="testOnBorrow" value="${redis_test_on_borrow}"></property>  </bean>  <!-- redis连接工厂 -->  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  <property name="hostName" value="${redis_addr}"></property>  <property name="port" value="${redis_port}"></property>  <property name="password" value="${redis_auth}"></property>  <property name="poolConfig" ref="jedisConfig"></property>  </bean>  <!-- redis操作模板,这里采用尽量面向对象的模板 -->  <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  <property name="connectionFactory" ref="connectionFactory" />  <!--  如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->  <property name="keySerializer">  <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  </property>  <property name="valueSerializer">  <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  </property>  </bean>
redis.properties:
#访问地址redis_addr=127.0.0.1#访问端口redis_port=6379#授权密码,有没有这一项取决于要连接的redis服务是否设置了此项redis_auth=#连接池的最大数据库连接数。设为0表示无限制redis_max_active=1024#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。redis_max_idle=200#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。redis_max_wait=10000#在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;redis_test_on_borrow=true


三、实现自己的redis客户端操作封装

/** * 封装对Redis的常用操作,尽量与Redis原始命令一致。Redis命令参考:http://doc.redisfans.com/ *  * @author robert * */@Servicepublic class RedisCache {@Autowiredprivate StringRedisTemplate redisTemplate;// redis操作模板// Key(键),简单的key-value操作/** * 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。 *  * @param key * @return */public long TTL(String key) {return redisTemplate.getExpire(key);}/** * 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key */public Set<String> KEYS(String pattern) {return redisTemplate.keys(pattern);}/** * 实现命令:DEL key,删除一个key *  * @param key */public void DEL(String key) {redisTemplate.delete(key);}// String(字符串)/** * 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key) *  * @param key * @param value */public void SET(String key, String value) {redisTemplate.opsForValue().set(key, value);}/** * 实现命令:SET key value EX seconds,设置key-value和超时时间(秒) *  * @param key * @param value * @param timeout *            (以秒为单位) */public void SET(String key, String value, long timeout) {redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);}/** * 实现命令:GET key,返回 key所关联的字符串值。 *  * @param key * @return value */public String GET(String key) {return redisTemplate.opsForValue().get(key);}// Hash(哈希表)/** * 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value *  * @param key * @param field * @param value */public void HSET(String key, String field, Object value) {redisTemplate.opsForHash().put(key, field, value);}/** * 实现命令:HGET key field,返回哈希表 key中给定域 field的值 *  * @param key * @param field * @return */public String HGET(String key, String field) {return (String) redisTemplate.opsForHash().get(key, field);}/** * 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。 *  * @param key * @param fields */public void HDEL(String key, Object... fields) {redisTemplate.opsForHash().delete(key, fields);}/** * 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。 *  * @param key * @return */public Map<Object, Object> HGETALL(String key) {return redisTemplate.opsForHash().entries(key);}// List(列表)/** * 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头 *  * @param key * @param value * @return 执行 LPUSH命令后,列表的长度。 */public long LPUSH(String key, String value) {return redisTemplate.opsForList().leftPush(key, value);}/** * 实现命令:LPOP key,移除并返回列表 key的头元素。 *  * @param key * @return 列表key的头元素。 */public String LPOP(String key) {return redisTemplate.opsForList().leftPop(key);}/** * 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。 *  * @param key * @param value * @return 执行 LPUSH命令后,列表的长度。 */public long RPUSH(String key, String value) {return redisTemplate.opsForList().rightPush(key, value);}/** * 实现命令:RPOP key,移除并返回列表 key的尾元素。 *  * @param key * @return 列表key的头元素。 */public String RPOP(String key) {return redisTemplate.opsForList().rightPop(key);}// Set(集合)/** * 实现命令:SADD key member,将一个 member元素加入到集合 key当中,已经存在于集合的 member元素将被忽略。 *  * @param key * @param member */public void SADD(String key, String member) {redisTemplate.opsForSet().add(key, member);}/** * 实现命令:SMEMBERS key,返回集合 key 中的所有成员。 *  * @param key * @return */public Set<String> SMEMEBERS(String key) {return redisTemplate.opsForSet().members(key);}// SortedSet(有序集合)/** * 实现命令:ZADD key score member,将一个 member元素及其 score值加入到有序集 key当中。 *  * @param key * @param score * @param member */public void ZADD(String key, double score, String member) {redisTemplate.opsForZSet().add(key, member, score);}/** * 实现命令:ZRANGE key start stop,返回有序集 key中,指定区间内的成员。 *  * @param key * @param start * @param stop * @return */public Set<String> ZRANGE(String key, double start, double stop) {return redisTemplate.opsForZSet().rangeByScore(key, start, stop);}/** * redis执行RedisCallback *  * @param pipelineCallback */public void execute(RedisCallback<List<Object>> pipelineCallback) {redisTemplate.execute(pipelineCallback);}}

至此,对redis的基本操作功能就已经实现了。

四、用redis实现spring cache

在spring-redis.xml中增加cacheManager注入
<!-- 配置缓存 -->     <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">           <constructor-arg name="template" ref="redisTemplate" />           <property name="expires">           <map>           <!-- 配置每个缓存块的失效时间,单位为秒 不配置则默认为0,表示永不过期-->                <entry key="cardInfoCache" value="86400"></entry>                </map>        </property>     </bean>
 在spring的主配置文件中增加cache的注解映射注释
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<cache:annotation-driven />

这样,redis就代替了spring的原始内存空间块去进行数据缓存。直接使用spring cache的注解(@Cacheable、@CachePut、@CacheEvict、@CacheConfig),代码上就变得简洁多了。

五、应用实例

1.初始化权限信息

/** * 初始权限加载到redis *  * @author robert * */public class InitPriviligeCacheBean {public static final Logger logger = LoggerFactory.getLogger(InitPriviligeCacheBean.class);@AutowiredApiUserAuthService apiUserAuthService;@Autowiredprivate RedisCache redisCache;/** * 采用redis的pipeline方式,对于批量redis指令操作,性能提升很大 *  * @throws Exception */public void priviligeInit() throws Exception {final List<ApiUserAuthKey> userAuthList = apiUserAuthService.selectAll();                final String priviligeKey = Constants.priModule4RedisHashKey;                RedisCallback<List<Object>> pipelineCallback = new RedisCallback<List<Object>>() {public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {connection.openPipeline();for (ApiUserAuthKey apiUserAuthKey : userAuthList) {                       //这里把商户id+apiId作为hashMap的key,去存储每个商户id对应apiId的权限,防止redis出现大量key情况下的key重复                                       String authApiField = apiUserAuthKey.getUserId+"_"+apiUserAuthKey .getApiId();                                       Integer authApiValue = apiUserAuthKey.getIsAuthed();                                       redisCache.HSET(priviligeKey , authApiField, authApiValue);}return connection.closePipeline();}};redisCache.execute(pipelineCallback);logger.info("privilige init to redis succcessful!");}}

<bean id="InitPriviligeCacheBean" class="***.***.***.***.***.InitPriviligeCacheBean" init-method="priviligeInit"> 

pipeline的原生代码为:
private static void usePipeline(int count){        Jedis jr = null;        try {            jr = new Jedis("127.0.0.1", 6379);            Pipeline pl = jr.pipelined();            for(int i =0; i<count; i++){                 pl.incr("testKey");            }                pl.sync();        } catch (Exception e) {            e.printStackTrace();        }finally{            if(jr!=null){                jr.disconnect();            }        }    }

2.spring cache注解使用

@Cacheable(value = "cardInfoCache", key = "'CardInfoService_'+#map.toString()")public String query(Map<String, Object> map) throws Exception {CardInfo cardInfo= cardInfoService.selectByPk(map);return cardInfo.toString();}










原创粉丝点击