redis的使用

可以存缓存,(可以直接用也可以用 spring cache)

可以存分布式锁: Redisson
可以session共享: spring-session

redis里面存数字,如验证码,就算存的时候用字符串存,但是取的时候也可能自动转成 数字!!!

取的时候封装成Object,后面再toString!!!

private boolean codeCorrect(String mobile,String code){
    Object redisCode = redisTemplate.opsForValue ().get ("MessageCode"+mobile);
    return code.equals (redisCode.toString ());
}

要在pom里面加依赖

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

application.properties配置地址和端口

spring.redis.host=localhost
spring.redis.port=6379

springboot 里面直接注入一般就可以使用了

常用函数:

    redisTemplate.opsForValue().get(key)
    redisTemplate.opsForValue().set(key,maxId, SECONDS_VAL, TimeUnit.SECONDS
    redisTemplate.opsForValue().increment(key) 值自增
BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(cartKey);
String productRedisValue = (String) operations.get(skuId.toString());

自动注入:【这个是,键和值都是字符串,值传JSON字符串,这样方便跨平台还有什么的】
@Autowired
private StringRedisTemplate redisTemplate;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

设置过期时间:

创建时设置:redisTemplate.opsForValue().set("catelogJSON", JSON.toJSONString(catelogJSON), 20, TimeUnit.MINUTES);
创建后设置:redisTemplate.expire("lock",10,TimeUnit.SECONDS);

删除锁(某一个kv)原子性操作:【使用Lua脚本】

String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
redisTemplate.execute(new DefaultRedisScript<Long>(script, Long.class), Arrays.asList("lock"), uuid);
@Autowired
private RedisTemplate<String, Object> redisTemplate;



// 获取最大id值
private int getMaxArticleId(String tableName){
    // 如果redis没有最大id,先去查数据库表,要是有,直接返回
    long SECONDS_VAL = 12000; // 20分钟,单位秒
    String key = tableName+"maxId";


    int maxId = 0;
    try{
        maxId = (int)redisTemplate.opsForValue().get(key);


        System.out.println("redis: "+maxId);
    }catch (Exception e){
        maxId = articleMapper.getMaxArticleId(tableName);
        redisTemplate.opsForValue().set(key,maxId, SECONDS_VAL, TimeUnit.SECONDS);


        System.out.println("mysql: "+maxId);
    }finally {
        return maxId;
    }
}


// 最大id值加一
private void incrMaxArticleId(String tableName){
    String key = tableName+"maxId";
    redisTemplate.opsForValue().increment(key);
}

无论能不能自动注入,都加上配置类 【对象序列化 用json】

package com.xxxx.zxg2.config;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/***

* @ClassName: RedisConfig  
*/
@Configuration
public class RedisConfig {
    
   @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        // key采用String的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        // value序列化方式采用jackson
        // 之前的写法 template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setValueSerializer(RedisSerializer.json());
        // hash的value序列化方式采用jackson
        // 之前的写法 template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(RedisSerializer.json());
        template.afterPropertiesSet();
        return template;
    }
   
}


版权声明:本文为weixin_45552441原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。