spring boot中使用resid-RedisTemplate
来源:互联网 发布:网络直播电视版 编辑:程序博客网 时间:2024/06/11 11:57
1、项目中导入依赖jar包 这里用的maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version></dependency>2、项目配置文件设置redis信息 这里用的 yml配置文件
redis: host: port: password: pool: max-active: 8 min-idle: 0 max-idle: 8 max-wait: 10000 timeout: 0database: 9注意 要在spring:节点下
3、在项目中新建类 让spring boot启动时加载 RedisTemplate
package com.prairie.manage.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.cache.CacheManager;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.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import java.lang.reflect.Method;/** * Created by xxx on 2017/6/25. * 加载redis配置信息 */@Configuration@EnableCachingpublic class RedisConfiguration { @Bean public KeyGenerator keyGenerator() { 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(); } }; } @SuppressWarnings("rawtypes") @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); //设置缓存过期时间 //rcm.setDefaultExpiration(60);//秒 return rcm; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); 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); template.afterPropertiesSet(); return template; }}4、新建redisService 实现具体操作redis
@Componentpublic class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; /** * key value 形式 * @param key * @param value */ @SuppressWarnings({"unchecked", "rawtypes"}) public void set(final String key, final String value) { redisTemplate.execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) throws DataAccessException { connection.set(key.getBytes(), value.getBytes()); return 1L; } }); } /** * set 一个有时间的key 到时间后自动过期 * @param key * @param value * @param liveTime 存活时间/秒 */ @SuppressWarnings({"unchecked", "rawtypes"}) public void set(final String key, final String value, final long liveTime) { redisTemplate.execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(key.getBytes(), liveTime, value.getBytes()); return 1L; } }); } /** * 根据key 获取value * @param key * @return */ @SuppressWarnings({"unchecked", "rawtypes"}) public String get(final String key) { return (String) redisTemplate.execute(new RedisCallback() { public Object doInRedis(RedisConnection connection) throws DataAccessException { try { return new String(connection.get(key.getBytes())); } catch (Exception e) { } return null; } }); } /** * 自增 key不存在则为1 key存在则自增 * @param key */ @SuppressWarnings({"unchecked", "rawtypes"}) public void incrby(final String key) { redisTemplate.execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) throws DataAccessException { Long incr = connection.incr(key.getBytes()); return incr; } }); } /** * 自增不存在数值会默认初始化0然后加1,传入时间后自动销毁 * @param key * @param liveTime */ public void incrby(final String key,final long liveTime) { redisTemplate.execute(new RedisCallback<Object>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { Long incr = connection.incr(key.getBytes()); connection.expire(key.getBytes(), liveTime); return incr; } }); } /** * 自减 * @param key */ @SuppressWarnings({"unchecked", "rawtypes"}) public void decrby(final String key) { redisTemplate.execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) throws DataAccessException { Long incr = connection.decr(key.getBytes()); return incr; } }); } /** * 删除key的value * @param key */ public void del(final String key){ redisTemplate.execute(new RedisCallback<Object>(){ public Long doInRedis(RedisConnection connection) throws DataAccessException { return connection.del(key.getBytes()); } }); } /** * 根据key查看是否存在 刷新 * @param key * @return */ public boolean hasKey(String key){ return redisTemplate.hasKey(key); } /** * 根据key刷新过期时间 * @param key * @param timeOut * @return */ public boolean refreshLiveTime(String key,long timeOut){ return redisTemplate.expire(key,timeOut, TimeUnit.SECONDS); }}
阅读全文
0 0
- spring boot中使用resid-RedisTemplate
- spring boot RedisTemplate的简单使用
- Spring-boot通过redisTemplate使用redis(无须手动序列化)
- spring boot data RedisTemplate 序列化问题
- 关于spring中使用redis中文乱码问题(redistemplate)
- Spring集成Redis使用RedisTemplate
- Spring RedisTemplate使用多种operation
- 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- 使用Spring-Data-Redis存储对象(redisTemplate)
- 使用spring redisTemplate.getValueSerializer()遇到的问题
- spring与redis整合,redisTemplate的使用
- Spring-Data-Redis之RedisTemplate的使用
- 使用Spring-Data-Redis存储对象(redisTemplate)
- Spring Data Redis(RedisTemplate的使用)
- Spring-Data-Redis之RedisTemplate的使用
- 查看正在执行的SQL
- 基于uploadfile的批量上传
- yii2高版-博客1-配置数据库 建表,注册用户
- ToLua学习笔记,获取全局lua表,局部lua表
- 定制化操作时用到的一些命令
- spring boot中使用resid-RedisTemplate
- CSS-BFC的概念及其作用
- final修饰符
- 使用ZXing生成QRcode二维码
- html5 div 模拟textarea
- 6.26~6.29模拟总结
- Yii2的高级应用程序模板中设置隐藏 index.php 步骤
- 关于使用Java实现的简单网络爬虫Demo
- 使用@ViewChild @ViewChildren(ngAfterViewInit), @ViewChild@ViewChildren(ngAfterContentInit)