redis 在spring boot工程中的应用(二)
来源:互联网 发布:怎么加淘宝商家微信号 编辑:程序博客网 时间:2024/04/29 07:17
http://wiselyman.iteye.com/blog/2184884 在spring boot 中加入redis换成的样例
http://www.tuicool.com/articles/qyEvYnR Spring Boot使用redis做数据缓存
http://www.myexception.cn/other/1938770.html Spring Boot使用redis作数据缓存
================ 以上都是基于注解,不是自定义的,因此不能满足我目前工作的需求
http://www.tuicool.com/articles/BFFB7r Spring+Jedis+Redis自定义模板实现缓存Object对象 :这个已经提供一种自定义的方法了
接下来介绍一下Redis一种实现方法:
先在application.properties中写入如下配置
spring.redis.database=0spring.redis.host=172.16.22.21spring.redis.password= # Login password of the redis server.spring.redis.pool.max-active=3000spring.redis.pool.max-idle=1000spring.redis.pool.max-wait=1500spring.redis.pool.min-idle=0spring.redis.port=6379spring.redis.sentinel.master= # Name of Redis server.spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.spring.redis.timeout=0
在configure中写入一下配置信息
import java.lang.reflect.Method;import org.springframework.beans.factory.annotation.Value;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.GenericToStringSerializer;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;@Configuration@EnableCachingpublic class CacheConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Bean public KeyGenerator wiselyKeyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean public JedisConnectionFactory JedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setTimeout(timeout); //设置连接超时时间 return factory; } //针对特定的类进行序列化缓存,定义对应的bean ============= 对于序列化会存在许多问题,我在第(三)中会慢慢做总结 @Bean(name="redisTemplateLocationMonitor") public RedisTemplate<String, LocationMonitor> redisTemplateLocationMonitor() { RedisTemplate<String, LocationMonitor> redisTemplate = new RedisTemplate<String, LocationMonitor>(); redisTemplate.setConnectionFactory(JedisConnectionFactory()); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new GenericToStringSerializer<Object>(Object.class)); return redisTemplate; } //这个函数是通用的,但是如果换成的类有多种,并且在一个service中要调用多种redisTemplate,就要写对应的Bean @Bean(name="redisTemplate") public RedisTemplate<Object, Object> redisTemplate() { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>(); redisTemplate.setConnectionFactory(JedisConnectionFactory()); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new GenericToStringSerializer<Object>(Object.class)); return redisTemplate; } @Bean(name="cacheManager") public CacheManager cacheManager() { return new RedisCacheManager(redisTemplate()); } public void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); } }以下是对应的自定义缓存方法:先看接口
public interface LocationMonitorRedis {/* * 持久化Redis中缓存的定位信息 */public void saveToPSQ();/* * 向Redis中缓存一条定位信息 */public void save(LocationMonitor locationMonitor);}
@Servicepublic class LocationMonitorRedisImpl implements LocationMonitorRedis {@Resourceprivate RedisTemplate<String, LocationMonitor> redisTemplate;private final LocationMonitorRepository locationMonitorRepository;@Autowiredpublic LocationMonitorRedisImpl(LocationMonitorRepository locationMonitorRepository) {this.locationMonitorRepository = locationMonitorRepository;}//把数据持久化(可以根据自己情况实现,这里用jpa)@Overridepublic void saveToPSQ() {String pattern = "bak.LocationMonitor.*"; //模式串Set<String> keys = redisTemplate.keys(pattern);//找出所有匹配的键List<LocationMonitor> lmList = new ArrayList<LocationMonitor>(); Iterator<String> it = keys.iterator();while(it.hasNext()){String key = it.next();LocationMonitor lm = redisTemplate.opsForValue().get(key);//取出所有对应的缓存类的数据lmList.add(lm);}locationMonitorRepository.save(lmList);//持久化redisTemplate.delete(keys);//删除键}@Overridepublic void save(LocationMonitor locationMonitor) {if(locationMonitor != null){Long now = System.currentTimeMillis(); String key = "bak.LocationMonitor."+locationMonitor.getId()+"."+now; //构建键值redisTemplate.opsForValue().set(key, locationMonitor);key = "LocationMonitor."+locationMonitor.getCorrectionAll().getId();redisTemplate.opsForValue().set(key, locationMonitor); //保存} else {System.out.println("传来的对象为空!!!");return ;}}}
1 0
- redis 在spring boot工程中的应用(二)
- redis 在spring boot工程中的应用(一)
- redis 在spring boot工程中的应用(四) 图片的读取与缓存 byte string
- redis 在spring boot工程中的应用(三) 问题发现与解决 :反序列化 报错
- Spring boot redis的使用(二)
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot 中的redis使用
- AOP以及其在Spring中的应用(二)
- AOP以及其在Spring中的应用(二)
- AOP及其在Spring中的应用(二)
- AOP及其在Spring中的应用(二)
- 《spring-boot学习》-05-spring boot中redis应用
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- redis在工程中的使用
- convolution layer
- java 二叉查找树(搜索树、排序树)实现
- phonegap开发——环境搭建
- LeetCode 45 - Jump Game II
- FsRtlIsNameInExpression失败原因
- redis 在spring boot工程中的应用(二)
- oracle 11g安装教程 win7 64位
- $@
- matlab中如何获取当前日期时间函数
- 软件开发模式对比(瀑布、迭代、螺旋、敏捷)
- Android:30分钟弄明白Touch事件分发机制
- 98.Which statement is true regarding the COALESCE function?
- VMProtect简单教程
- Python学习笔记-列表1