SpringBoot+Redis的简单示例

Redis是一种常见的运行在内存的键值数据库,以字符串类型为中心,能够支持多种数据类型,包括字符串,散列表,列表(链表),集合,有序集合。
下面我们具体示范SpringBoot中操作Redis。
先安利一款美观免费的Redis可视化操作工具:https://github.com/qishibo/AnotherRedisDesktopManager/releases

首先先引入maven坐标:

		<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>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

我们使用jedis连接驱动。
在application.prperties中添加配置

spring.cache.type=redis
spring.cache.cache-names=rediscache
spring.redis.host=localhost  #redis服务器地址
spring.redis.port=6379		#端口
#jedis连接池
spring.redis.jedis.pool.max-idle=8	
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=2000ms

配置好后可以直接依赖注入使用RedisTemplate

 @Autowired
 private RedisTemplate redisTemplate;

RedisTemplate中封装了大量的opsForxxx方法用来操作数据,每一个方法对应一种数据类型,如:
在这里插入图片描述这些方法返回的RedisOperation内又有操作具体数据类型的方法,方法名都与redis命令名相同,如果对redis命令比较熟悉的话可以轻松使用:
在这里插入图片描述

当我们查看redis中的键值时可能会发现不是我们想象的字符串:
在这里插入图片描述
这是因为redis默认使用jdk序列化器,我们可以在配置类里配置redis序列化器:

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory factory)
    {
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //配置默认序列化器
        redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
//        redisTemplate.setDefaultSerializer(redisTemplate.getStringSerializer());
        return redisTemplate;
    }
}

在开发中我们还可以通过缓存注解操作redis,使用@CachePut,@Cacheable ,@CacheEvict,@EnableCaching这四个注解。
先把@EnableCaching放在启动类上

@SpringBootApplication
@EnableCaching
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class RedisAnnotationService {
	//将返回结果放入缓存,value与application.properties中的
	//spring.cache.cache-names=rediscache相同
    @CachePut(value = "rediscache", key = "'user_'+#result.id",condition = "#result!='null'")
    public User insertUser(User user)
    {
        System.out.println("cache put------------------");
        return user;
    }
    //先从缓存中查找,如果没有,调用方法查询,将查询结果放入缓存
    @Cacheable(value = "rediscache", key="'user_'+#id",condition = "#result!='null'")
    public User getUser(Integer id)
    {
        System.out.println("get user-------------------------------");
        return new User(id,"he");
    }
    //将缓存失效
    @CacheEvict(value = "rediscache", key="'user_'+#id")
    public void deleteUser(Integer id)
    {
        System.out.println("delete user----------------------------");
    }
}

要注意缓存对象要实现Serializable接口,否则无法序列化

public class User implements Serializable {

    private Integer id;
    private String name;
    ..省略构造函数, get set
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值