Spring Boot 集成Redis 实现增删改查

本文介绍了如何在Spring Boot项目中集成Redis,通过详细步骤实现了Redis的增删改查功能,包括添加相关依赖,配置文件设置,创建Redis工具类以及编写测试用例。
摘要由CSDN通过智能技术生成

spring boot 集成redis 实现CRUD

1:添加pom依赖

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

        <!-- json 格式 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.38</version>
        </dependency>

        <!-- redis客户端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2:添加redis加载类

package com.config;


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
@Conditi
在Java Spring Boot中使用Memcached作为缓存技术,可以帮助提高应用性能,通过减少数据库访问次数。以下是Spring Boot集成Memcached的基本操作: 1. 添加依赖:首先,你需要在你的`pom.xml`文件中添加Spring Data Memcached和Memcached客户端库的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> </dependency> ``` 2. 配置 Memcached:在`application.properties`或`application.yml`中添加Memcached的配置,例如主机名和端口。 ```properties spring.data.memcached.servers=localhost:11211 ``` 3. 使用`@Cacheable`注解进行缓存读取(增): 在方法上添加`@Cacheable`,Spring会尝试从Memcached获取数据,如果存在则返回,不存在则执行实际的方法并存入缓存。 ```java @GetMapping("/cache") @Cacheable("myCacheKey") public String fetchData() { // 实际的业务逻辑 } ``` 4. 使用`@CachePut`注解进行缓存更新(改): 如果你想更新缓存中的值,可以使用`@CachePut`。这会在缓存中找到指定键的值后,执行你的更新操作。 ```java @PostMapping("/update") @CachePut(value = "myCacheKey", key = "#id") public void updateData(@RequestBody MyEntity entity) { // 更新数据逻辑 } ``` 5. 使用`@CacheEvict`注解进行缓存删除(删): 如果需要从缓存中移除某个键的数据,可以使用`@CacheEvict`。 ```java @DeleteMapping("/delete") @CacheEvict(value = "myCacheKey", key = "#id") public void deleteData(@PathVariable Long id) { // 删除数据逻辑 } ``` 6. 使用`@CacheConfig`和`@Caching`进行批量操作(查): 如果需要对多个键进行缓存操作,可以使用`@Caching`注解,并在`@CacheConfig`中定义缓存策略。 ```java @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { return new SimpleCacheManager(Arrays.asList( new RedisCacheConfiguration("myOtherCache"))); } } @GetMapping("/batchFetch") @Caching(caches = {"myOtherCache"}) public List<String> fetchManyData() { // 执行多个查询操作 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值