application.yml
spring : application : name : demoredis : data : host : localhostport : 6379 database : 0 password : timeout : 10slettuce : pool : min-idle : 0 max-idle : 8 max-active : 8 max-wait : - 1ms
依赖
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-data-redis</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-test</ artifactId> < scope> test</ scope> </ dependency>
序列化配置
package com. example. demo. config ; import com. fasterxml. jackson. annotation. JsonAutoDetect ;
import com. fasterxml. jackson. annotation. PropertyAccessor ;
import com. fasterxml. jackson. databind. ObjectMapper ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. data. redis. connection. RedisConnectionFactory ;
import org. springframework. data. redis. core. RedisTemplate ;
import org. springframework. data. redis. serializer. Jackson2JsonRedisSerializer ;
import org. springframework. data. redis. serializer. StringRedisSerializer ; @Configuration
public class RedisConfig { @Bean @SuppressWarnings ( "all" ) public RedisTemplate < String , Object > redisTemplate ( RedisConnectionFactory redisConnectionFactory) { RedisTemplate < String , Object > template = new RedisTemplate < > ( ) ; template. setConnectionFactory ( redisConnectionFactory) ; Jackson2JsonRedisSerializer jsonRedisSerializer= new Jackson2JsonRedisSerializer ( Object . class ) ; ObjectMapper objectMapper= new ObjectMapper ( ) ; objectMapper. setVisibility ( PropertyAccessor . ALL, JsonAutoDetect. Visibility . ANY) ; objectMapper. enableDefaultTyping ( ObjectMapper. DefaultTyping . NON_FINAL) ; jsonRedisSerializer. setObjectMapper ( objectMapper) ; StringRedisSerializer stringRedisSerializer= new StringRedisSerializer ( ) ; template. setKeySerializer ( stringRedisSerializer) ; template. setHashKeySerializer ( stringRedisSerializer) ; template. setValueSerializer ( jsonRedisSerializer) ; template. setHashValueSerializer ( jsonRedisSerializer) ; template. afterPropertiesSet ( ) ; return template; }
}
工具类
package com. example. demo. utils ; 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. Component ; import java. util. * ;
import java. util. concurrent. TimeUnit ;
@SuppressWarnings ( value = { "unchecked" , "rawtypes" } )
@Component
public class RedisCache
{ @Autowired public RedisTemplate redisTemplate; public < T > void setCacheObject ( final String key, final T value) { redisTemplate. opsForValue ( ) . set ( key, value) ; } public < T > void setCacheObject ( final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { redisTemplate. opsForValue ( ) . set ( key, value, timeout, timeUnit) ; } public boolean expire ( final String key, final long timeout) { return expire ( key, timeout, TimeUnit . SECONDS) ; } public boolean expire ( final String key, final long timeout, final TimeUnit unit) { return redisTemplate. expire ( key, timeout, unit) ; } public long getExpire ( final String key) { return redisTemplate. getExpire ( key) ; } public Boolean hasKey ( String key) { return redisTemplate. hasKey ( key) ; } public < T > T getCacheObject ( final String key) { ValueOperations < String , T > operation = redisTemplate. opsForValue ( ) ; return operation. get ( key) ; } public boolean deleteObject ( final String key) { return redisTemplate. delete ( key) ; } public boolean deleteObject ( final Collection collection) { return redisTemplate. delete ( collection) > 0 ; } public < T > long setCacheList ( final String key, final List < T > dataList) { Long count = redisTemplate. opsForList ( ) . rightPushAll ( key, dataList) ; return count == null ? 0 : count; } public < T > List < T > getCacheList ( final String key) { return redisTemplate. opsForList ( ) . range ( key, 0 , - 1 ) ; } 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; } public < T > Set < T > getCacheSet ( final String key) { return redisTemplate. opsForSet ( ) . members ( key) ; } public < T > void setCacheMap ( final String key, final Map < String , T > dataMap) { if ( dataMap != null ) { redisTemplate. opsForHash ( ) . putAll ( key, dataMap) ; } } public < T > Map < String , T > getCacheMap ( final String key) { return redisTemplate. opsForHash ( ) . entries ( key) ; } public < T > void setCacheMapValue ( final String key, final String hKey, final T value) { redisTemplate. opsForHash ( ) . put ( key, hKey, value) ; } public < T > T getCacheMapValue ( final String key, final String hKey) { HashOperations < String , String , T > opsForHash = redisTemplate. opsForHash ( ) ; return opsForHash. get ( key, hKey) ; } public < T > List < T > getMultiCacheMapValue ( final String key, final Collection < Object > hKeys) { return redisTemplate. opsForHash ( ) . multiGet ( key, hKeys) ; } public boolean deleteCacheMapValue ( final String key, final String hKey) { return redisTemplate. opsForHash ( ) . delete ( key, hKey) > 0 ; } public Collection < String > keys ( final String pattern) { return redisTemplate. keys ( pattern) ; }
}
测试类
RedisCache
package com. example. demo. test ; import com. example. demo. utils. RedisCache ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import org. springframework. data. redis. core. * ; import java. util. * ;
import java. util. concurrent. TimeUnit ; @SpringBootTest
class RedisCacheTest { @Autowired RedisCache redisCache; @Autowired RedisTemplate redisTemplate; private static final String TEST_KEY = "test:list:key" ; @Test public void setObject ( ) { redisCache. setCacheObject ( "object" , "test" , 10 , TimeUnit . SECONDS) ; } @Test public void setTimeOut ( ) { redisCache. setCacheObject ( "object" , "test" ) ; redisCache. expire ( "object" , 100 , TimeUnit . SECONDS) ; System . out. println ( redisCache. getExpire ( "object" ) ) ; ; } @Test public void check ( ) { System . out. println ( redisCache. hasKey ( "object" ) ) ; } @Test public void getObject ( ) { System . out. println ( ( String ) redisCache. getCacheObject ( "object" ) ) ; } @Test public void deleteObject ( ) { redisCache. setCacheObject ( "obj" , "1" ) ; System . out. println ( ( String ) redisCache. getCacheObject ( "obj" ) ) ; redisCache. deleteObject ( "obj" ) ; System . out. println ( ( String ) redisCache. getCacheObject ( "obj" ) ) ; } @Test public void getList ( ) { List < String > testData = Arrays . asList ( "item1" , "item2" , "item3" ) ; redisCache. setCacheList ( TEST_KEY, testData) ; List < Object > result = redisCache. getCacheList ( TEST_KEY) ; for ( Object o : result) { System . out. println ( o) ; } } @Test public void getSet ( ) { Set < String > set = Set . of ( "a" , "b" ) ; redisCache. setCacheSet ( "TestSet" , set) ; Set < Object > cacheSet = redisCache. getCacheSet ( "TestSet" ) ; for ( Object o : cacheSet) { System . out. println ( o) ; } } @Test public void getMap ( ) { Map < String , String > stringStringMap = new HashMap < > ( ) ; stringStringMap. put ( "name" , "zww" ) ; stringStringMap. put ( "age" , "21" ) ; redisCache. setCacheMap ( "TestMap" , stringStringMap) ; Map < String , Object > testMap = redisCache. getCacheMap ( "TestMap" ) ; System . out. println ( testMap. get ( "name" ) ) ; System . out. println ( testMap. get ( "age" ) ) ; } @Test public void getHash ( ) { redisCache. setCacheMapValue ( "hashMap" , "testHashMap" , "1" ) ; redisCache. setCacheMapValue ( "hashMap" , "testHashMap2" , "2" ) ; System . out. println ( ( String ) redisCache. getCacheMapValue ( "hashMap" , "testHashMap" ) ) ; System . out. println ( ( String ) redisCache. getCacheMapValue ( "hashMap" , "testHashMap2" ) ) ; } }
RedisTemplate
package com. example. demo. test ; import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import org. springframework. data. redis. connection. DataType ;
import org. springframework. data. redis. core. * ; import java. util. List ;
import java. util. Set ;
import java. util. concurrent. TimeUnit ; @SpringBootTest
public class RedisTemplateTest { @Autowired RedisTemplate redisTemplate; @Test void contextLoads ( ) { String mannor = ( String ) redisTemplate. opsForValue ( ) . get ( "mannor" ) ; System . out. println ( mannor) ; redisTemplate. opsForValue ( ) . set ( "k1" , "v1" , 10L , TimeUnit . SECONDS) ; Boolean aBoolean = redisTemplate. opsForValue ( ) . setIfAbsent ( "mannor1" , "mannor" ) ; System . out. println ( aBoolean) ; } @Test public void hashTest ( ) { HashOperations hashOperations = redisTemplate. opsForHash ( ) ; hashOperations. put ( "002" , "name" , "zhangsan" ) ; hashOperations. put ( "002" , "age" , "20" ) ; hashOperations. put ( "002" , "addr" , "beijing" ) ; Object age = hashOperations. get ( "002" , "age" ) ;
Set keys = hashOperations. keys ( "002" ) ; for ( Object key : keys) {
} List values = hashOperations. values ( "002" ) ; for ( Object value : values) { System . out. println ( value) ; } } @Test public void listTest ( ) { ListOperations listOperations = redisTemplate. opsForList ( ) ; listOperations. leftPush ( "list" , "00" ) ; listOperations. leftPushAll ( "list" , "01" , "02" , "03" ) ; List list = listOperations. range ( "list" , 0 , - 1 ) ; for ( Object val : list) { System . out. println ( val) ; } System . out. println ( "------------------------------------------------------------" ) ; Long size = listOperations. size ( "list" ) ; for ( int i = 0 ; i < size; i++ ) { String element = ( String ) listOperations. rightPop ( "list" ) ; System . out. println ( element) ; } } @Test public void testSet ( ) { SetOperations setOperations = redisTemplate. opsForSet ( ) ; setOperations. add ( "myset" , "a" , "b" , "c" , "a" ) ; Set < String > myset = setOperations. members ( "myset" ) ; for ( String o : myset) { System . out. println ( o) ; } setOperations. remove ( "myset" , "a" , "b" ) ; myset = setOperations. members ( "myset" ) ; for ( String o : myset) { System . out. println ( o) ; } } @Test public void testZset ( ) { ZSetOperations zSetOperations = redisTemplate. opsForZSet ( ) ; zSetOperations. add ( "myZset" , "a" , 10.0 ) ; zSetOperations. add ( "myZset" , "b" , 11.0 ) ; zSetOperations. add ( "myZset" , "c" , 12.0 ) ; zSetOperations. add ( "myZset" , "a" , 13.0 ) ; Set < String > myZset = zSetOperations. range ( "myZset" , 0 , - 1 ) ; for ( String s : myZset) { System . out. println ( s) ; } zSetOperations. incrementScore ( "myZset" , "b" , 20.0 ) ; myZset = zSetOperations. range ( "myZset" , 0 , - 1 ) ; for ( String s : myZset) { System . out. println ( s) ; } zSetOperations. remove ( "myZset" , "a" , "b" ) ; myZset = zSetOperations. range ( "myZset" , 0 , - 1 ) ; for ( String s : myZset) { System . out. println ( s) ; } } @Test public void testCommon ( ) { Set < String > keys = redisTemplate. keys ( "*" ) ; for ( String key : keys) { System . out. println ( key) ; } Boolean itcast = redisTemplate. hasKey ( "itcast" ) ; System . out. println ( itcast) ; redisTemplate. delete ( "myZset" ) ; DataType dataType = redisTemplate. type ( "myset" ) ; System . out. println ( dataType. name ( ) ) ; }
}