前提条件是安装了Redis,若未安装请参考:Windows10安装RedisKongbaiw的博客CSDN博客win10安装redis 一、引入依赖!redis缓存操作dependencygroupIdorg。springframework。bootgroupIdspringbootstarterdataredisartifactIddependency!pool对象池dependencygroupIdorg。apache。commonsgroupIdcommonspool2artifactIddependency 二、配置Redisspring:redis配置redis:地址host:127。0。0。1端口,默认为6379port:6379密码password:666666连接超时时间timeout:180slettuce:pool:连接池中的最小空闲连接minidle:0连接池中的最大空闲连接maxidle:8连接池的最大数据库连接数maxactive:8连接池最大阻塞等待时间(使用负值表示没有限制)maxwait:1ms 三、Redis工具类importjava。util。Set;importjava。util。List;importjava。util。Map;importjava。util。Collection;importjava。util。concurrent。TimeUnit;importorg。springframework。stereotype。Component;importorg。springframework。data。redis。core。RedisTemplate;importorg。springframework。data。redis。core。HashOperations;importorg。springframework。data。redis。core。ValueOperations;springredis工具类ComponentSuppressWarnings(value{unchecked,rawtypes})publicclassRedisCache{publicfinalRedisTemplateredisTemplate;publicRedisCache(RedisTemplateredisTemplate){this。redisTemplateredisTemplate;}缓存基本的对象,Integer、String、实体类等paramkey缓存的键值paramvalue缓存的值publicTvoidsetCacheObject(finalStringkey,finalTvalue){redisTemplate。opsForValue()。set(key,value);}缓存基本的对象,Integer、String、实体类等paramkey缓存的键值paramvalue缓存的值paramtimeout时间paramtimeUnit时间颗粒度publicTvoidsetCacheObject(finalStringkey,finalTvalue,finalIntegertimeout,finalTimeUnittimeUnit){redisTemplate。opsForValue()。set(key,value,timeout,timeUnit);}设置有效时间paramkeyRedis键paramtimeout超时时间returntrue设置成功;false设置失败publicbooleanexpire(finalStringkey,finallongtimeout){returnexpire(key,timeout,TimeUnit。SECONDS);}设置有效时间paramkeyRedis键paramtimeout超时时间paramunit时间单位returntrue设置成功;false设置失败publicbooleanexpire(finalStringkey,finallongtimeout,finalTimeUnitunit){returnBoolean。TRUE。equals(redisTemplate。expire(key,timeout,unit));}获得缓存的基本对象paramkey缓存键值return缓存键值对应的数据publicTTgetCacheObject(finalStringkey){ValueOperationsString,ToperationredisTemplate。opsForValue();returnoperation。get(key);}删除单个对象paramkeyRedis键publicbooleandeleteObject(finalStringkey){returnBoolean。TRUE。equals(redisTemplate。delete(key));}删除集合对象paramcollection多个对象publiclongdeleteObject(finalCollectioncollection){returnredisTemplate。delete(collection);}缓存List数据paramkey缓存的键值paramdataList待缓存的List数据return缓存的对象publicTlongsetCacheList(finalStringkey,finalListTdataList){LongcountredisTemplate。opsForList()。rightPushAll(key,dataList);returncountnull?0:count;}获得缓存的list对象paramkey缓存的键值return缓存键值对应的数据publicTListTgetCacheList(finalStringkey){returnredisTemplate。opsForList()。range(key,0,1);}缓存Setparamkey缓存键值paramdataSet缓存的数据return缓存数据的对象publicTlongsetCacheSet(finalStringkey,finalSetTdataSet){LongcountredisTemplate。opsForSet()。add(key,dataSet);returncountnull?0:count;}获得缓存的setparamkey缓存键值publicTSetTgetCacheSet(finalStringkey){returnredisTemplate。opsForSet()。members(key);}缓存Mapparamkey缓存键值paramdataMap缓存的数据publicTvoidsetCacheMap(finalStringkey,finalMapString,TdataMap){if(dataMap!null){redisTemplate。opsForHash()。putAll(key,dataMap);}}获得缓存的Mapparamkey缓存键值publicTMapString,TgetCacheMap(finalStringkey){returnredisTemplate。opsForHash()。entries(key);}往Hash中存入数据paramkeyRedis键paramhKeyHash键paramvalue值publicTvoidsetCacheMapValue(finalStringkey,finalStringhKey,finalTvalue){redisTemplate。opsForHash()。put(key,hKey,value);}获取Hash中的数据paramkeyRedis键paramhKeyHash键returnHash中的对象publicTTgetCacheMapValue(finalStringkey,finalStringhKey){HashOperationsString,String,TopsForHashredisTemplate。opsForHash();returnopsForHash。get(key,hKey);}获取多个Hash中的数据paramkeyRedis键paramhKeysHash键集合returnHash对象集合publicTListTgetMultiCacheMapValue(finalStringkey,finalCollectionObjecthKeys){returnredisTemplate。opsForHash()。multiGet(key,hKeys);}获得缓存的基本对象列表parampattern字符串前缀return对象列表publicCollectionStringkeys(finalStringpattern){returnredisTemplate。keys(pattern);}} 四、调用Redis工具类方法初始化数据AutowiredprivateRedisCacheredisCache;项目启动时,初始化数据到缓存PostConstructpublicvoidinit(){booleanisDeletedredisCache。deleteObject(test);if(!isDeleted){redisCache。setCacheObject(test,Hello,World);}} 路漫漫其修远兮,吾将上下而求索 译文:在追寻真理方面,前方的道路还很漫长,但我将百折不挠,不遗余力地去追求和探索。 如果您有什么好的想法与方法,欢迎在评论区留言,我们一起讨论