当前位置: 首页 > news >正文

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

实现步骤

  1. 配置文件添加
    添加application.yaml新增Redis缓存链接,如下:
redis:host: ipport: 端口
  1. 开启缓存
    1. 主启动类添加
@EnableCaching
  1. 在实现类的方法中添加缓存
    1. 主要在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") 
  1. 实体类序列化
    直接在class类上添加
implement Serializable

禁用redis只读副本

CONFIG SET slave-read-only no

相关文章:

  • [特殊字符] PostgreSQL MCP 开发指南
  • ruoyi中如何使用Public来存储静态资源
  • 全国青少年信息素养大赛 C++算法创意实践挑战赛初赛 集训模拟试卷《四》及详细答案解析
  • 03.Python代码NumPy-通过numPy来创建不同数组
  • 【Qt】Qt 按钮控件详解,PushButton,RadioButton,CheckBox,ToolButton
  • 韩金明董事长受邀出席“个人经济体高质量发展暨私域直播行业合规”调研活动
  • 长亭红队技术面试题
  • springboot中的事务失效(自调用)
  • 基于YOLO11的跌倒检测报警系统
  • 【Linux】su、su-、sudo、sudo -i、sudo su - 命令有什么区别?分别适用什么场景?
  • 儿童内侧颞叶癫痫伴海马硬化的体素形态学分析和机器学习分类
  • Vue —— 实用的工具函数
  • Bp靶场 - Jwt
  • OceanBases数据库单机社区版保姆级安装
  • CNN:卷积到底做了什么?
  • 解决ubuntu安装软件时候deb文件的闪退问题
  • cas 5.3单点登录中心开发手册
  • 深度学习Y5周:yolo.py文件解读
  • LeetCode每日一题4.16
  • 使用CubeMX新建EXTI外部中断工程——使用回调函数
  • 成都培训学校网站建设/万网官网入口
  • 网站备案号被注销什么原因/广州seo网站管理
  • 做代理哪个网站靠谱吗/bt磁力搜索引擎在线
  • 做网站 教程/市场营销策略有哪4种
  • 网站的交互设计包括哪些/搜索风云榜
  • 做网站和平台多少钱/做网站找哪家好