spring-data集成redis使用 数据集合池
2017-07-03 17:16
405 查看
接口中返回数据,使用redis做缓存处理,可以提交程序的并发能力
接口压测对比:
有redis缓存 (优化后:120秒内启动10000个线程) 错误率、吞吐量明显比没redis的好很多
没有redis (优化前:120秒内启动10000个线程)
1、redis list集合池用法
当数据在insert 进数据时,同步到redis缓存中 ,(key,list<object>) an需求设置list集合的大小,这里我们可以它list设计成一个数据池
当数据条数达到峰值时,进行溢出处理或者移除部分,这样可以防止数据累积导致redis过大;
因为key list存放的是集合不好给list里的每条记录设置生命周期
2、定时任务移除历史数据,同步行数据
下面项目例子
项目中redis使用接口实现
spring配置集成
maven依赖中引入
接口压测对比:
有redis缓存 (优化后:120秒内启动10000个线程) 错误率、吞吐量明显比没redis的好很多
没有redis (优化前:120秒内启动10000个线程)
1、redis list集合池用法
当数据在insert 进数据时,同步到redis缓存中 ,(key,list<object>) an需求设置list集合的大小,这里我们可以它list设计成一个数据池
当数据条数达到峰值时,进行溢出处理或者移除部分,这样可以防止数据累积导致redis过大;
因为key list存放的是集合不好给list里的每条记录设置生命周期
2、定时任务移除历史数据,同步行数据
下面项目例子
项目中redis使用接口实现
package com.wamei.dao.impl; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import com.wamei.entity.NewsFlash; import org.apache.log4j.Logger; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; /** * 快讯redis缓存逻辑 * Created by qixuan.chen on 2017/6/29. */ @Service("newFlashRedisDao") public class NewFlashRedisDaoImpl implements com.wamei.dao.NewFlashRedisDao { @Resource(name="redisTemplate") private ZSetOperations<String,String> zsetOps; @Resource(name="redisTemplate") private RedisTemplate<String, Object> redisTemplate; private final static Logger logger = Logger.getLogger(NewFlashRedisDaoImpl.class); @Override public void saveObject(String key, Object object) { redisTemplate.opsForHash().put(key.hashCode()+"", key, object); } @Override public void saveKeyValue(String key, Object object) { redisTemplate.opsForValue().set(key,object); } @Override public Object getKeyValue(String key) { return redisTemplate.opsForValue().get(key); } @Override public Object fetchObject(String key) { return redisTemplate.opsForHash().get(key.hashCode()+"", key); } @Override public void deleteObject(String key) { redisTemplate.opsForHash().delete(key.hashCode()+"", key); } @Override public void deleteViewCache(String key) { List<Object> list = redisTemplate.opsForList().range(key, 0, -1); if(null!=list && !list.isEmpty()){ for(int i=0;i<list.size();i++){ redisTemplate.opsForList().remove(key, i, list.get(i)); } } } /** * 设置超时时间 * @param key * @param seconds */ @Override public void expire(String key, int seconds){ zsetOps.getOperations().expire(key, seconds, null); } /** * 设置超时时间 * @param key * @param tempTime */ @Override public void setExpire(String key, int tempTime) { redisTemplate.expire(key,tempTime, TimeUnit.SECONDS); } @Override public void saveListLimit(String key, NewsFlash obj,int max) { long len = zsetOps.size(key); if(len>max){ //超出限制、清空部分旧数据 zsetOps.removeRange(key,len-90,len); } SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); String json = JSONObject.toJSONString(obj, filter); Date factTime; try{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); factTime = sdf.parse(obj.getCreateTimeStr()); }catch (Exception e){ factTime = obj.getCreateTime(); } long score = genScoreByFactTime(factTime); zsetOps.add(key, json, score); logger.info("快讯存放到redis:"+zsetOps.size(key)); } /** * 根据发布时间生成Score * @param factTime 发布时间 * @return */ private long genScoreByFactTime(Date factTime){ long timeIndex = factTime.getTime(); return -timeIndex; } @Override public List<NewsFlash> fetchByPage(String key, long start, long step) { List<NewsFlash> list = new ArrayList<NewsFlash>(); //通过数组下标来取缓存的值 Set<ZSetOperations.TypedTuple<String>> datas = zsetOps.rangeWithScores(key, start, start+step-1); for (ZSetOperations.TypedTuple<String> typedTuple : datas) { NewsFlash newsFlash= JSONObject.parseObject(typedTuple.getValue(), NewsFlash.class); list.add(newsFlash); } return list; } @Override public void saveList(String key,List<NewsFlash> value){ Iterator<NewsFlash> data = value.iterator(); Integer n=0; while(data.hasNext()) { // zsetOps.add(key, data.next(), n); String jsonStr = JSONObject.toJSONString(data.next()); zsetOps.add(key, jsonStr, n); n++; } } }
spring配置集成
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!--[redis-JedisPoolConfig配置](http://blog.csdn.net/liang_love_java/article/details/50510753)--> <!-- jedis-2.7.2.jar 依赖jar包 commons-pool2-2.3.jar jedis基于 commons-pool2-2.3.jar 自己实现了一个资源池。 配置参数 详见 http://blog.csdn.net/liang_love_java/article/details/50510753 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="1" /> <property name="maxTotal" value="5" /> <property name="blockWhenExhausted" value="true" /> <property name="maxWaitMillis" value="30000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="192.168.1.200" /> <!-- <property name="hostName" value="192.168.1.200" /> --> <property name="port" value="6379"/> <property name="password" value="123456"/> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="usePool" value="true"/> <property name="timeout" value="100000" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean> </beans>
maven依赖中引入
<!-- spring-redis NoSql begin --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency> <!-- spring-redis NoSql end -->
相关文章推荐
- 【Redis】spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 【Redis】Redis的基本安装及使用、Jedis的基本使用、spring-data-redis的集成、主从模式、哨兵模式
- Spring-data-redis集成提交数据出现'maxActive'和‘maxWaitMillis’错误
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 大数据之mongodb --> (2)java集成 MongoDB 3.2,使用Spring-data-mongodb进行集成
- spring-data-redis使用自定义序列化数据 使用 protobuf
- spring data Redis 集成jedis操作 redis中的数据。
- 7、redis之使用spring集成commons-pool来操作常见数据类型
- redis与spring集成--不使用spring-data-redis
- 使用 Spring data redis 结合 Spring cache 缓存数据配置
- spring-data-redis 使用
- spring data redis 集群(sentinel实现)和simple spring memcached分布式初使用
- 使用spring-data-redis做缓存
- 使用Spring-Data-Redis存储对象(redisTemplate)
- spring-data-redis使用RedisTemplate模板存储时键值与预设不一致的解决方法
- Redis 集成Spring(spring-data-redis)