久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

瀏覽:81日期:2023-03-01 18:44:08
目錄1 Redis2 Maven依賴3 application.propertis4 RedisConfig5 RedisService6 調(diào)試代碼 7 調(diào)試結(jié)果1 Redis

Redis 是完全開源的,遵守 BSD 協(xié)議,是一個(gè)高性能的 key-value 數(shù)據(jù)庫。

Redis 與其他 key - value 緩存產(chǎn)品有以下三個(gè)特點(diǎn):

(1)Redis支持?jǐn)?shù)據(jù)的持久化,可以將內(nèi)存中的數(shù)據(jù)保存在磁盤中,重啟的時(shí)候可以再次加載進(jìn)行使用。

(2)Redis不僅僅支持簡單的key-value類型的數(shù)據(jù),同時(shí)還提供list,set,zset,hash等數(shù)據(jù)結(jié)構(gòu)的存儲。

(2)Redis支持?jǐn)?shù)據(jù)的備份,即master-slave模式的數(shù)據(jù)備份。

2 Maven依賴

<!--redis緩存--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> <!-- 阿里JSON解析器 --><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version></dependency>3 application.propertis

# Redis數(shù)據(jù)庫索引(默認(rèn)為0)spring.redis.database=0# Redis服務(wù)器地址spring.redis.host=127.0.0.1# Redis服務(wù)器連接端口spring.redis.port=6379# Redis服務(wù)器連接密碼(默認(rèn)為空)spring.redis.password=# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)spring.redis.jedis.pool.max-active=20# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)spring.redis.jedis.pool.max-wait=-1# 連接池中的最大空閑連接spring.redis.jedis.pool.max-idle=10# 連接池中的最小空閑連接spring.redis.jedis.pool.min-idle=0# 連接超時(shí)時(shí)間(毫秒)spring.redis.timeout=10004 RedisConfig

Redis配置文件。

package com.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;import org.springframework.context.annotation.*;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer; @Configurationpublic class RedisConfig { @Bean @SuppressWarnings(value = { 'unchecked', 'rawtypes' }) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory); FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);// 使用StringRedisSerializer來序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer); // Hash的key也采用StringRedisSerializer的序列化方式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer); template.afterPropertiesSet();return template; }}5 RedisService

Redis基本操作方法。

package com.service; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.BoundSetOperations;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service; import java.util.*;import java.util.concurrent.TimeUnit; @Servicepublic class RedisService { @Autowired private RedisTemplate redisTemplate; /** * 緩存基本的對象,Integer、String、實(shí)體類等 * * @param key 緩存的鍵值 * @param value 緩存的值 */ public <T> void setCacheObject(final String key, final T value) {redisTemplate.opsForValue().set(key, value); } /** * 緩存基本的對象,Integer、String、實(shí)體類等 * * @param key 緩存的鍵值 * @param value 緩存的值 * @param timeout 時(shí)間 * @param timeUnit 時(shí)間顆粒度 */ public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {redisTemplate.opsForValue().set(key, value, timeout, timeUnit); } /** * 設(shè)置有效時(shí)間 * * @param key Redis鍵 * @param timeout 超時(shí)時(shí)間 * @return true=設(shè)置成功;false=設(shè)置失敗 */ public boolean expire(final String key, final long timeout) {return expire(key, timeout, TimeUnit.SECONDS); } /** * 設(shè)置有效時(shí)間 * * @param key Redis鍵 * @param timeout 超時(shí)時(shí)間 * @param unit 時(shí)間單位 * @return true=設(shè)置成功;false=設(shè)置失敗 */ public boolean expire(final String key, final long timeout, final TimeUnit unit) {return redisTemplate.expire(key, timeout, unit); } /** * 獲得緩存的基本對象。 * * @param key 緩存鍵值 * @return 緩存鍵值對應(yīng)的數(shù)據(jù) */ public <T> T getCacheObject(final String key) {ValueOperations<String, T> operation = redisTemplate.opsForValue();return operation.get(key); } /** * 刪除單個(gè)對象 * * @param key */ public boolean deleteObject(final String key) {return redisTemplate.delete(key); } /** * 刪除集合對象 * * @param collection 多個(gè)對象 * @return */ public long deleteObject(final Collection collection) {return redisTemplate.delete(collection); } /** * 緩存List數(shù)據(jù) * * @param key 緩存的鍵值 * @param dataList 待緩存的List數(shù)據(jù) * @return 緩存的對象 */ public <T> long setCacheList(final String key, final List<T> dataList) {Long count = redisTemplate.opsForList().rightPushAll(key, dataList);return count == null ? 0 : count; } /** * 獲得緩存的list對象 * * @param key 緩存的鍵值 * @return 緩存鍵值對應(yīng)的數(shù)據(jù) */ public <T> List<T> getCacheList(final String key) {return redisTemplate.opsForList().range(key, 0, -1); } /** * 緩存Set * * @param key 緩存鍵值 * @param dataSet 緩存的數(shù)據(jù) * @return 緩存數(shù)據(jù)的對象 */ public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);Iterator<T> it = dataSet.iterator();while (it.hasNext()) { setOperation.add(it.next());}return setOperation; } /** * 獲得緩存的set * * @param key * @return */ public <T> Set<T> getCacheSet(final String key) {return redisTemplate.opsForSet().members(key); } /** * 緩存Map * * @param key * @param dataMap */ public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {if (dataMap != null) { redisTemplate.opsForHash().putAll(key, dataMap);} } /** * 獲得緩存的Map * * @param key * @return */ public <T> Map<String, T> getCacheMap(final String key) {return redisTemplate.opsForHash().entries(key); } /** * 往Hash中存入數(shù)據(jù) * * @param key Redis鍵 * @param hKey Hash鍵 * @param value 值 */ public <T> void setCacheMapValue(final String key, final String hKey, final T value) {redisTemplate.opsForHash().put(key, hKey, value); } /** * 獲取Hash中的數(shù)據(jù) * * @param key Redis鍵 * @param hKey Hash鍵 * @return Hash中的對象 */ public <T> T getCacheMapValue(final String key, final String hKey) {HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();return opsForHash.get(key, hKey); } /** * 獲取多個(gè)Hash中的數(shù)據(jù) * * @param key Redis鍵 * @param hKeys Hash鍵集合 * @return Hash對象集合 */ public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {return redisTemplate.opsForHash().multiGet(key, hKeys); } /** * 獲得緩存的基本對象列表 * * @param pattern 字符串前綴 * @return 對象列表 */ public Collection<String> keys(final String pattern) {return redisTemplate.keys(pattern); }}6 調(diào)試代碼

package com.controller; import com.service.RedisService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*; @RestController@RequestMapping('/redis')public class RedisController { @Autowired private RedisService redisService; /** * 保存Redis數(shù)據(jù) * * @param key * @param value * @return */ @PostMapping('/setValue/{key}/{value}') public String setValue(@PathVariable('key') String key, @PathVariable('value') String value) {redisService.setCacheObject(key, value);return '保存成功'; } /** * 獲取Redis數(shù)據(jù) * * @param key * @return */ @GetMapping('/getValue') public String getValue(@RequestParam String key) {return redisService.getCacheObject(key); } /** * 刪除Redis數(shù)據(jù) * * @param key * @return */ @DeleteMapping('/deleteValue/{key}') public String deleteValue(@PathVariable('key') String key) {return redisService.deleteObject(key) ? '刪除成功' : '刪除失敗'; }}7 調(diào)試結(jié)果

保存數(shù)據(jù):

SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

獲取數(shù)據(jù):

SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

刪除數(shù)據(jù):

SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

到此這篇關(guān)于SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SpringBoot Redis保存獲取和刪除數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 中文字幕视频免费观看 | 久热中文在线 | 亚洲欧美一区二区三区四区 | 成人三级av | 99re在线视频精品 | 日韩二三区| 日产精品久久久一区二区 | av超碰| 国产精品第一国产精品 | 91中文在线| 精品无码久久久久久国产 | 啵啵羞羞影院 | 久久精品播放 | 黄色一级免费看 | 国产精品一区二区在线观看 | 国产成人精品免高潮在线观看 | 人人看人人射 | 成年人福利 | 欧美一级播放 | 国产在线a | 精品xxxx户外露出视频 | 欧美伊人 | 国产视频一二区 | 一区二区成人在线 | 国产电影精品久久 | 久久亚洲一区二区三区四区 | 日韩超级大片免费看国产国产播放器 | 曰批视频在线观看 | 99久久99热这里只有精品 | 涩久久 | 欧美视频二区 | 久久伊人免费视频 | 二区三区 | 国产区视频在线观看 | 91在线中文 | 毛片a片| 国产精品成av人在线视午夜片 | 成人福利视频网 | 欧美一级c片 | 久久aⅴ国产欧美74aaa | 日韩高清一区二区 |