SpringBoot整合redis实现缓存

介绍:

           redis是一个key-value存储系统,它支持存储的value类型,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序,为了保证效率,数据都是缓存在内存中。redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。Redis 是一个高性能的key-value数据库。

1:SpringBoot引入redis依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

1.application.properties文件下进行配置

#地址
spring.redis.host=127.0.0.1
#端口
spring.redis.port=6379
#redis默认密码123456
spring.redis.password=123456

2:编写redis配置类

package simulationvirtual.VirtualExperiment.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @program: Virtual_Experiment
 * @description: redis相关配置
 * @author: Xiongch
 * @create: 2022-09-10 13:44
 **/
@Configuration
public class redisConfig {
    //连接工厂
   @Resource
   private final RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
    @Bean(name = "RedisTemplate")
    public RedisTemplate<String,Object> RedisTemplate(){

        RedisTemplate<String, Object> redistemplate = new RedisTemplate<String, Object>();

        //注入连接工厂
        redistemplate.setConnectionFactory(redisConnectionFactory);
        //建(key)序列化
        redistemplate.setKeySerializer(new StringRedisSerializer());
        //值(value)序列化t
        redistemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //hash类型key序列化
        redistemplate.setHashKeySerializer(new StringRedisSerializer());
        //hash类型value序列化
        redistemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redistemplate;
    }
}

3:操作代码示例

    //获取RedisTemplate操作对象,接下来的redis数据库操作用到它
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

   //对数据写入
   redisTemplate.opsForValue().set(uuid,imagecode,2, TimeUnit.MINUTES);
   //读出通过get()方法
  

3.1:Redis中opsForValue()方法的使用介绍

1,set(K key, V value)

增加一个字符串类型的值,key是键,value是值

redisTemplate.opsForValue().set("stringValue","bbb");  

2,get(Object key)

获取key键对应的值

String stringValue = redisTemplate.opsForValue().get("key")

3,append(K key, String value)

原有的值基础上新增字符串到末尾

redisTemplate.opsForValue().append("key", "appendValue");
String stringValueAppend = redisTemplate.opsForValue().get("key");
System.out.println("通过append(K key, String value)方法修改后的字符串:"+stringValueAppend);  

4、getAndSet(K key, V value)

获取原来key键对应的值并重新赋新值。

String oldAndNewStringValue = redisTemplate.opsForValue().getAndSet("key", "ccc");  
System.out.print("通过getAndSet(K key, V value)方法获取原来的值:" + oldAndNewStringValue );  
String newStringValue = redisTemplate.opsForValue().get("key");  
System.out.println("修改过后的值:"+newStringValue);  

5、get(K key, long start, long end)

截取key键对应值得字符串,从开始下标位置开始到结束下标的位置(包含结束下标)的字符串

String cutString = redisTemplate.opsForValue().get("key", 0, 3);  
System.out.println("通过get(K key, long start, long end)方法获取截取的字符串:"+cutString);  

6、size(K key)

获取指定字符串的长度

Long stringValueLength = redisTemplate.opsForValue().size("key");  
System.out.println("通过size(K key)方法获取字符串的长度:"+stringValueLength);  

7、setIfAbsent(K key, V value)

如果键不存在则新增,存在则不改变已经有的值

boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentKey","fff");  
System.out.println("通过setIfAbsent(K key, V value)方法判断变量值absentValue是否存在:" + absentBoolean);  
if(absentBoolean){  
    String absentValue = redisTemplate.opsForValue().get("absentKey")+"";  
    System.out.print(",不存在,则新增后的值是:"+absentValue);  
    boolean existBoolean = redisTemplate.opsForValue().setIfAbsent("absentKey","eee");  
    System.out.print(",再次调用setIfAbsent(K key, V value)判断absentValue是否存在并重新赋值:" + existBoolean);  
    if(!existBoolean){  
        absentValue = redisTemplate.opsForValue().get("absentKey")+"";  
        System.out.print("如果存在,则重新赋值后的absentValue变量的值是:" + absentValue);  

8、set(K key, V value, long timeout, TimeUnit unit)

设置变量值的过期时间。

redisTemplate.opsForValue().set("timeOutKey", "timeOut", 5, TimeUnit.SECONDS);  
String timeOutValue = redisTemplate.opsForValue().get("timeOutKey")+"";  
System.out.println("通过set(K key, V value, long timeout, TimeUnit unit)方法设置过期时间,过期之前获取的数据:"+timeOutValue);  
Thread.sleep(5*1000);  
timeOutValue = redisTemplate.opsForValue().get("timeOutKey")+"";  
System.out.print(",等待10s过后,获取的值:"+timeOutValue);  

9:更多信息见官网Spring Data Redis

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小怪猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值