Springboot框架整合添加redis緩存功能
Hello大家好,本章我們添加redis緩存功能 。另求各路大神指點(diǎn),感謝
一:安裝Redis
因本人電腦是windows系統(tǒng),從https://github.com/ServiceStack/redis-windows下載了兼容windows系統(tǒng)的redis
下載后直接解壓到自定義目錄,運(yùn)行cmd命令,進(jìn)入到這個(gè)文件夾,在這個(gè)文件夾下運(yùn)行下面命令,啟動redis服務(wù)器
redis-server redis.windows.conf
運(yùn)行下面命令進(jìn)行測試:redis-cli
二:添加Redis依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.5.RELEASE</version> </dependency>
然后鼠標(biāo)右鍵選擇Maven→Reimport進(jìn)行依賴下載
三:添加Redis配置信息
在application.properties
中添加
spring.redis.host=127.0.0.1
spring.redis.port=6379? ?
spring.redis.timeout=0
spring.redis.password=
四:創(chuàng)建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; } }
五:創(chuàng)建Redis常用方法
RedisService
package com.example.demo.service; import java.util.List; /** * @author 張瑤 * @Description: redis常用方法 * @date 2018/4/30 10:35 */ public interface RedisService { /** * 設(shè)置給定 key 的值。如果 key 已經(jīng)存儲其他值, SET 就覆寫舊值,且無視類型。 * @param key * @param value * @return */ boolean set(String key, String value); /** * 獲取指定 key 的值。如果 key 不存在,返回 nil 。如果key 儲存的值不是字符串類型,返回一個(gè)錯(cuò)誤。 * @param key * @return */ String get(String key); /** * 設(shè)置 key 的過期時(shí)間。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); /** * 將一個(gè)或多個(gè)值插入到列表頭部。 如果 key 不存在,一個(gè)空列表會被創(chuàng)建并執(zhí)行 LPUSH 操作。 * 當(dāng) key 存在但不是列表類型時(shí),返回一個(gè)錯(cuò)誤。 * @param key * @param obj * @return */ long lpush(String key, Object obj); /** * 將一個(gè)或多個(gè)值插入到列表的尾部(最右邊)。 * 如果列表不存在,一個(gè)空列表會被創(chuàng)建并執(zhí)行 RPUSH 操作。 當(dāng)列表存在但不是列表類型時(shí),返回一個(gè)錯(cuò)誤。 * @param key * @param obj * @return */ long rpush(String key, Object obj); /** * 移除并返回列表的第一個(gè)元素。 * @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; } }
六:接口測試
創(chuàng)建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
項(xiàng)目地址
碼云地址:https://gitee.com/beany/mySpringBoot
GitHub地址:https://github.com/MyBeany/mySpringBoot
寫文章不易,如對您有幫助,請幫忙點(diǎn)下star
結(jié)尾
添加redis緩存功能已完成,后續(xù)功能接下來陸續(xù)更新,另求各路大神指點(diǎn),感謝大家。
到此這篇關(guān)于Springboot框架整合添加redis緩存功能的文章就介紹到這了,更多相關(guān)Springboot整合redis緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決fastjson從1.1.41升級到1.2.28后報(bào)錯(cuò)問題詳解
這篇文章主要介紹了解決fastjson從1.1.41升級到1.2.28后報(bào)錯(cuò)問題詳解,需要的朋友可以參考下2020-02-02Java輕量級權(quán)限認(rèn)證框架Sa-Token的使用
Sa-Token是一個(gè)輕量級Java權(quán)限認(rèn)證框架,本文就詳細(xì)的來介紹一下Java輕量級權(quán)限認(rèn)證框架Sa-Token的使用,主要解決:登錄認(rèn)證、權(quán)限認(rèn)證、Session會話、單點(diǎn)登錄、OAuth2.0、微服務(wù)網(wǎng)關(guān)鑒權(quán)等,感興趣的可以了解一下2022-03-03SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐
數(shù)據(jù)庫分庫分表中間件是采用的 apache sharding。本文主要介紹了SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09