Springboot整合redis缓存
mr_初晨 人气:0Hello大家好,本章我们添加redis缓存功能 。另求各路大神指点,感谢
一:安装Redis
因本人电脑是windows系统,从https://github.com/ServiceStack/redis-windows下载了兼容windows系统的redis
下载后直接解压到自定义目录,运行cmd命令,进入到这个文件夹,在这个文件夹下运行下面命令,启动redis服务器
redis-server redis.windows.conf
运行下面命令进行测试:redis-cli
二:添加Redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.5.RELEASE</version> </dependency>
然后鼠标右键选择Maven→Reimport进行依赖下载
三:添加Redis配置信息
在application.properties
中添加
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=0
spring.redis.password=
四:创建RedisConfigurer
package com.example.demo.core.configurer; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import redis.clients.jedis.JedisPoolConfig; /** * @author 张瑶 * @Description: redis配置 * @date 2018/4/30 10:28 */ @Configuration @EnableCaching //开启缓存 public class RedisConfigurer extends CachingConfigurerSupport { @Bean @ConfigurationProperties(prefix="spring.redis") public JedisPoolConfig getRedisConfig(){ JedisPoolConfig config = new JedisPoolConfig(); return config; } @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory getConnectionFactory(){ JedisConnectionFactory factory = new JedisConnectionFactory(); JedisPoolConfig config = getRedisConfig(); factory.setPoolConfig(config); return factory; } @Bean public RedisTemplate<?, ?> getRedisTemplate(){ RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory()); return template; } }
五:创建Redis常用方法
RedisService
package com.example.demo.service; import java.util.List; /** * @author 张瑶 * @Description: redis常用方法 * @date 2018/4/30 10:35 */ public interface RedisService { /** * 设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 * @param key * @param value * @return */ boolean set(String key, String value); /** * 获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。 * @param key * @return */ String get(String key); /** * 设置 key 的过期时间。key 过期后将不再可用。 * @param key * @param expire * @return */ boolean expire(String key, long expire); /** * 存集合 * @param key * @param list * @param <T> * @return */ <T> boolean setList(String key, List<T> list); /** * 取集合 * @param key * @param clz * @param <T> * @return */ <T> List<T> getList(String key, Class<T> clz); /** * 将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 * 当 key 存在但不是列表类型时,返回一个错误。 * @param key * @param obj * @return */ long lpush(String key, Object obj); /** * 将一个或多个值插入到列表的尾部(最右边)。 * 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 * @param key * @param obj * @return */ long rpush(String key, Object obj); /** * 移除并返回列表的第一个元素。 * @param key * @return */ String lpop(String key); /** * 删除已存在的键。不存在的 key 会被忽略。 * @param key * @return */ long del(final String key); }
RedisServiceImpl
package com.example.demo.service.impl; import com.alibaba.fastjson.JSON; import com.example.demo.service.RedisService; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import java.util.concurrent.TimeUnit; /** * @author 张瑶 * @Description: redis配置 * @date 2018/4/30 10:38 */ @Service public class RedisServiceImpl implements RedisService { @Resource private RedisTemplate<String, ?> redisTemplate; @Override public boolean set(final String key, final String value) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); connection.set(serializer.serialize(key), serializer.serialize(value)); return true; } }); return result; } @Override public String get(final String key){ String result = redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); byte[] value = connection.get(serializer.serialize(key)); return serializer.deserialize(value); } }); return result; } @Override public long del(final String key){ long result = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); long value = connection.del(serializer.serialize(key)); return value; } }); return result; } @Override public boolean expire(final String key, long expire) { return redisTemplate.expire(key, expire, TimeUnit.SECONDS); } @Override public <T> boolean setList(String key, List<T> list) { String value = JSON.toJSONString(list); return set(key,value); } @Override public <T> List<T> getList(String key,Class<T> clz) { String json = get(key); if(json!=null){ List<T> list = JSON.parseArray(json, clz); return list; } return null; } @Override public long lpush(final String key, Object obj) { final String value = JSON.toJSONString(obj); long result = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); long count = connection.lPush(serializer.serialize(key), serializer.serialize(value)); return count; } }); return result; } @Override public long rpush(final String key, Object obj) { final String value = JSON.toJSONString(obj); long result = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); long count = connection.rPush(serializer.serialize(key), serializer.serialize(value)); return count; } }); return result; } @Override public String lpop(final String key) { String result = redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); byte[] res = connection.lPop(serializer.serialize(key)); return serializer.deserialize(res); } }); return result; } }
六:接口测试
创建RedisController
package com.example.demo.controller; import com.example.demo.core.ret.RetResponse; import com.example.demo.core.ret.RetResult; import com.example.demo.service.RedisService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author 张瑶 * @Description: * @date 2018/4/30 11:28 */ @RestController @RequestMapping("redis") public class RedisController { @Resource private RedisService redisService; @PostMapping("/setRedis") public RetResult<String> setRedis(String name) { redisService.set("name",name); return RetResponse.makeOKRsp(name); } @PostMapping("/getRedis") public RetResult<String> getRedis() { String name = redisService.get("name"); return RetResponse.makeOKRsp(name); } }
输入http://localhost:8080/redis/setRedis
输入http://localhost:8080/redis/getRedis
项目地址
码云地址:https://gitee.com/beany/mySpringBoot
GitHub地址:https://github.com/MyBeany/mySpringBoot
写文章不易,如对您有帮助,请帮忙点下star
结尾
添加redis缓存功能已完成,后续功能接下来陆续更新,另求各路大神指点,感谢大家。
加载全部内容