Java实现Redis
String类型
代码
package com.whop.changyuan2.redisTest;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;import static org.assertj.core.api.Assertions.assertThat;@SpringBootTest
public class RedisString {@Autowiredprivate StringRedisTemplate redisTemplate;// 测试数据String key = "StringTest";String value = "Hello this is String , Redis!";@Testpublic void redisStringadd(){redisTemplate.opsForValue().set(key,value);}@Testpublic void redis2() {// 获取值并验证String retrievedValue = redisTemplate.opsForValue().get(key);System.out.println("Retrieved value: " + retrievedValue);assertThat(retrievedValue).isEqualTo(value);}@Testpublic void redis3() {// 清理测试数据redisTemplate.delete(key);}}
List
package com.whop.changyuan2.redisTest;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;import java.util.ArrayList;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class) // 确保测试类在 Spring 上下文中运行
public class RedisList {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void redisListAdd() {// 创建一个列表List<String> list = new ArrayList<>();list.add("list1实现");list.add("list2实现");list.add("list3实现");// 将列表中的元素逐个插入到Redis List中for (String s : list) {System.out.println("Inserting into Redis List: " + s);redisTemplate.opsForList().rightPush("listTest", s);}// 验证插入成功Long size = redisTemplate.opsForList().size("listTest");System.out.println("Redis List Size: " + size);}@Testpublic void redisListGet() {// 从Redis中获取列表中的元素List<Object> list = redisTemplate.opsForList().range("listTest", 0, -1);// 遍历并打印列表中的元素System.out.println("Elements from Redis List Site:"+list.size());for (Object obj : list) {System.out.println("Element from Redis List: " + obj);}}/*** 测试方法:redisListDel* 该方法用于从Redis列表中删除指定的元素。* 具体流程如下:* 1. 从Redis中获取指定列表的所有元素。* 2. 遍历列表,查找与指定值匹配的元素。* 3. 如果找到匹配的元素,则从列表中删除该元素。** 该方法没有参数,也没有返回值。*/@Testpublic void redisListDel() {// 从Redis中获取列表中的所有元素List<Object> list = redisTemplate.opsForList().range("listTest", 0, -1);// 遍历列表,查找并删除指定元素for (int i = 0; i < list.size(); i++) {Object o = list.get(i);if (o.equals("list1实现")) {System.out.println("删除 " + o);// 从Redis列表中删除匹配的元素redisTemplate.opsForList().remove("listTest", i, o);}else {System.out.println("else:"+o);}}}}
Hash
package com.whop.changyuan2.redisTest;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest
@RunWith(SpringRunner.class) // 确保测试类在 Spring 上下文中运行
public class RedisHash {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void redisHashAdd() {redisTemplate.opsForHash().put("hash", "key1", "value1");redisTemplate.opsForHash().put("hash", "key2", "value2");redisTemplate.opsForHash().put("hash", "key3", "value3");}@Testpublic void redisHashGet() {Object value = redisTemplate.opsForHash().get("hash", "key2");System.out.println("Value for key1: " + value);}@Testpublic void redisHashDel() {redisTemplate.opsForHash().delete("hash", "key2");}}
Set
package com.whop.changyuan2.redisTest;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisSet {@Autowiredprivate StringRedisTemplate redisTemplate;@Testpublic void redisSetAdd() {redisTemplate.opsForSet().add("setTest", "1", "2", "3", "4", "5");}@Testpublic void redisSetGet() {System.out.println(redisTemplate.opsForSet().members("setTest"));}@Testpublic void redisSetDel() {redisTemplate.opsForSet().remove("setTest", "1");}
}
ZSet
package com.whop.changyuan2.redisTest;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisZset {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void redisZsetAdd() {redisTemplate.opsForZSet().add("zsetTest", "a", 1);redisTemplate.opsForZSet().add("zsetTest", "b", 2);redisTemplate.opsForZSet().add("zsetTest", "c", 3);}@Testpublic void redisZsetGet() {System.out.println(redisTemplate.opsForZSet().range("zsetTest", 0, -1));}@Testpublic void redisZsetDel() {redisTemplate.opsForZSet().remove("zsetTest", "c");}
}
工具包
由于java数据存储到Redis中是二进制的,可以去找工具包实现序列化
RedisConfig
SpringBoot实现Redis缓存常用注解
@EnableCaching
开关性注解,在项目启动类或某个配置类上使用此注解后,则表示允许使用注解进行缓存操作@Cacheable
可用于类或者方法上;在目标方法前执行,会根据key先去缓存中查询看是否有数据,有就直接换回缓存中的key
对应的value值,不再执行目标方法,并将方法的返回值作为value,并以键值对的形式存入缓存。@CacheEvict
可用于类或方法上;在执行完目标方法后,并将方法的返回值作为value,并以键值对的形式存入缓存中。@CachePut
可用于类或方法上;在执行完目标方法后,清楚缓存中对应的key的数据(如果缓存中有对应的缓存的话)。@Caching
此注解即可作为 @Cacheable @CacheEvict @CachePut 三种注解中任何一直ongoing或几种来使用。@CacheConfig
可以用于配置 @Cacheable、@CacheEvict、@CachePut这三个注解的一些公共属性,例如cacheNames、
KeyGeneator
实现步骤
- 配置文件添加
添加application.yaml新增Redis缓存链接,如下:
redis:host: ipport: 端口
- 开启缓存
-
- 主启动类添加
@EnableCaching
- 在实现类的方法中添加缓存
-
- 主要在impl添加
// 主要用于查询操作
// 将redis有key就是用缓存,如果redis没有key就实现数据库查询并数据提交到redis里
@Cacheable(cacheNames = "ad-items-skus",key = "#id")// 主要用于修改操作
// 每一次都去查询数据库将数据库的数据提交到缓存中去
@CachePut(cacheNames = "ad-items-skus",key = "#id")// 主要用于删除操作
// 从redis中删除
@CacheEvict(cacheNames = "ad-items-skus",key = "#id") // 由于所有都是使用的 ad-items-skus 公共的可以在class上加入注解 可以修改为
@CacheConfig(cacheName = "ad-items-skus")
// 下面可以修改为
@Cacheable(key = "#id")
@CachePut(key = "#id")
@CacheEvict(key = "#id")
- 实体类序列化
直接在class类上添加
implement Serializable
禁用redis只读副本
CONFIG SET slave-read-only no