jcjssl

1、pom.xm

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

2、application.properties

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0  
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379  
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8  
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1  
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8  
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0  
# 连接超时时间(毫秒)
spring.redis.timeout=0  

3.RedisConfig.class

package com.redis.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;


@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };

    }

    @Bean
    public CacheManager cacheManager(
            @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(
            RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        RedisSerializer<String> keySerializer = new StringRedisSerializer();
        template.setKeySerializer(keySerializer);
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

4、Redis共同方法

4.1 指定缓存失效时间

 /**
     * 指定缓存失效时间
     * 如果没有存在的key,也不会报错
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        Boolean expire = redisTemplate.expire(SAVEKEY, 60, TimeUnit.SECONDS);
    }

4.2  根据key 获取过期时间

 /**
     * 根据key 获取过期时间
     * 如果没有存在的key或者key没有设置过期时间,执行此方法不会报错,而是返回0
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        Long aLong = redisTemplate.getExpire(SAVEKEY, TimeUnit.SECONDS);
        System.out.println("过期时间" + aLong);
    }

4.3  判断key是否存在

/**
     * 判断key是否存在
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        Boolean aBoolean = redisTemplate.hasKey(SAVEKEY);
        System.out.println(aBoolean);
    }

4.4  删除缓存(删除单个key)

/**
     * 删除缓存(删除单个key)
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        redisTemplate.delete(SAVEKEY);
    }

4.5  删除缓存(删除多个key)

/**
     * 删除缓存(删除多个key)
     * @throws Exception
     */
    @Test
    public void test5() throws Exception {
        String key[] = {SAVEKEY,SAVEKEY1};
        redisTemplate.delete(CollectionUtils.arrayToList(key));
    }

5、String

5.1  普通缓存获取

/**
     * 普通缓存获取
     * @throws Exception
     */
    @Test
    public void test6() throws Exception {
        Object result = redisTemplate.opsForValue().get(SAVEKEY);
        System.out.println(result);
    }

5.2  普通缓存放入

/**
     * 普通缓存放入
     * @throws Exception
     */
    @Test
    public void test7() throws Exception {
        redisTemplate.opsForValue().set(SAVEKEY, "111");
        redisTemplate.opsForValue().set(SAVEKEY1, "222");
    }

5.3  普通缓存放入并设置时间

 /**
     * 普通缓存放入并设置时间
     * @throws Exception
     */
    @Test
    public void test8() throws Exception {
        redisTemplate.opsForValue().set(SAVEKEY, "111", 60, TimeUnit.SECONDS);
    }

5.4  递增(要增加几(大于0))

/**
     * 递增(要增加几(大于0))
     * @throws Exception
     */
    @Test
    public void test9() throws Exception {
        Long increment = redisTemplate.opsForValue().increment(SAVEKEY, 5);
        System.out.println(increment);
    }

5.5  递减(要减少几(小于0))

 /**
     * 递减(要减少几(小于0))
     * @throws Exception
     */
    @Test
    public void test10() throws Exception {
        Long increment = redisTemplate.opsForValue().increment(SAVEKEY, -5);
        System.out.println(increment);
    }

 6、Map

6.1 HashSet(将map放入缓存)

/**
     * HashSet(将map放入缓存)
     * @throws Exception
     */
    @Test
    public void test11() throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("aaa","111");
        map.put("bbb","222");
        map.put("ccc","333");
        map.put("ddd","444");
        map.put("eee","555");
        redisTemplate.opsForHash().putAll(SAVEKEY, map);
    }

6.2 获取hashKey对应的所有键值

/**
     * 获取hashKey对应的所有键值
     * @throws Exception
     */
    @Test
    public void test12() throws Exception {
        Map map = redisTemplate.opsForHash().entries(SAVEKEY);
        System.out.println(map);
    }

6.3 HashSet 并设置时间(将map放入缓存并设置时间)

/**
     * HashSet 并设置时间(将map放入缓存并设置时间)
     * @throws Exception
     */
    @Test
    public void test13() throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("aaa","111");
        map.put("bbb","222");
        map.put("ccc","333");
        map.put("ddd","444");
        map.put("eee","555");
        redisTemplate.opsForHash().putAll(SAVEKEY, map);
        redisTemplate.expire(SAVEKEY,60,TimeUnit.SECONDS);
    }

6.4 HashGet(获取map中指定key的值)

 /**
     * HashGet(获取map中指定key的值)
     * @throws Exception
     */
    @Test
    public void test14() throws Exception {
        Object result = redisTemplate.opsForHash().get(SAVEKEY, "aaa");
        System.out.println(result);
    }

6.5 向一张hash表中放入数据,如果不存在将创建,如果存在,则改变它的值(向map中再添加数据)

 /**
     * 向一张hash表中放入数据,如果不存在将创建,如果存在,则改变它的值(向map中再添加数据)
     * @throws Exception
     */
    @Test
    public void test15() throws Exception {
        redisTemplate.opsForHash().put(SAVEKEY, "fff", "666");
    }

6.6 向一张hash表中放入数据,如果不存在将创建,如果存在,则改变它的值,并且设置有效时间(向map中再添加数据)

/**
     * 向一张hash表中放入数据,如果不存在将创建,如果存在,则改变它的值,并且设置有效时间(向map中再添加数据)
     * @throws Exception
     */
    @Test
    public void test16() throws Exception {
        redisTemplate.opsForHash().put(SAVEKEY, "fff", "666");
        redisTemplate.expire(SAVEKEY,60,TimeUnit.SECONDS);
    }

6.7 删除hash表中的值(删除map中的一个值或多个值)

 /**
     * 删除hash表中的值(删除map中的一个值或多个值)
     * @throws Exception
     */
    @Test
    public void test17() throws Exception {
        //删除单个
        redisTemplate.opsForHash().delete(SAVEKEY, "fff");
        //删除多个
        List<String> list = new ArrayList<>();
        list.add("ddd");
        list.add("eee");
        redisTemplate.opsForHash().delete(SAVEKEY, list.toArray());
    }

6.8 判断hash表中是否有该项的值(判断map中是否存在这样的一个值)

 /**
     * 判断hash表中是否有该项的值(判断map中是否存在这样的一个值)
     * @throws Exception
     */
    @Test
    public void test18() throws Exception {
        Boolean result = redisTemplate.opsForHash().hasKey(SAVEKEY, "ccc");
        Boolean result1 = redisTemplate.opsForHash().hasKey(SAVEKEY, "fff");
        System.out.println(result);
        System.out.println(result1);
    }

6.9 hash递增 如果不存在,就会创建一个 并把新增后的值返回

/**
     * hash递增 如果不存在,就会创建一个 并把新增后的值返回
     * @throws Exception
     */
    @Test
    public void test19() throws Exception {
        redisTemplate.opsForHash().increment(SAVEKEY, "ddd", 5);
    }

6.10 hash递减

/**
     * hash递减
     * @throws Exception
     */
    @Test
    public void test20() throws Exception {
        redisTemplate.opsForHash().increment(SAVEKEY, "ddd", -5);
    }

7、Set

7.1 将set数据放入缓存

/**
     * 将set数据放入缓存
     * @throws Exception
     */
    @Test
    public void test21() throws Exception {
        Set set = new HashSet();
        set.add("aaa");
        set.add("bbb");
        set.add("ccc");
        redisTemplate.opsForSet().add(SAVEKEY, set.toArray());
    }

7.2 根据key获取Set中的所有值

 /**
     * 根据key获取Set中的所有值
     * @throws Exception
     */
    @Test
    public void test22() throws Exception {
        Set set = redisTemplate.opsForSet().members(SAVEKEY);
        set.forEach(o -> System.out.println(o));
    }

7.3 根据value从一个set中查询,是否存在

 /**
     * 根据value从一个set中查询,是否存在
     * @throws Exception
     */
    @Test
    public void test23() throws Exception {
        Boolean result = redisTemplate.opsForSet().isMember(SAVEKEY, "aaa");
        System.out.println(result);
    }

7.4 将数据放入set缓存

/**
     * 将数据放入set缓存
     * @throws Exception
     */
    @Test
    public void test24() throws Exception {
        redisTemplate.opsForSet().add(SAVEKEY, "ddd");
    }

7.5 获取set缓存的长度

/**
     * 获取set缓存的长度
     * @throws Exception
     */
    @Test
    public void test25() throws Exception {
        Long size = redisTemplate.opsForSet().size(SAVEKEY);
        System.out.println("set缓存的长度" + size);
    }

7.6 移除值为value的

/**
     * 移除值为value的
     * @throws Exception
     */
    @Test
    public void test26() throws Exception {
        redisTemplate.opsForSet().remove(SAVEKEY, "ddd");
    }

 

分类:

技术点:

相关文章: