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

StringRedisTemplate简单操作redis

效果图

image

效果图

image

 效果图

image

 

 完整pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.java</groupId><artifactId>redis-service</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 继承 Spring Boot 父 POM,管理 Spring Boot 依赖版本 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version><relativePath/> <!-- 从仓库查找父 POM --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version></dependency><!--引入junit单元测试依赖--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

  代码部分

image

 

package com.java;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Main {public static void main(String[] args) {SpringApplication.run(Main.class,args);}
}

  

package com.java;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import java.util.Set;
import java.util.concurrent.TimeUnit;/*** @Description:* @Author: tutu-qiuxie* @Create: 2026/2/4 0:43*/
@Service
public class RedisAdminService {@Autowiredprivate StringRedisTemplate redisTemplate;/** 查询 key */public Set<String> keys(String pattern) {return redisTemplate.keys(pattern);}/** 获取值 */public String get(String key) {return redisTemplate.opsForValue().get(key);}/** 设置值 */public void set(String key, String value, long seconds) {redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);}/** 删除 */public void delete(String key) {redisTemplate.delete(key);}/** TTL */public Long ttl(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}
}

  

package com.java;import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;import java.util.concurrent.TimeUnit;/*** @Description:* @Author: tutu-qiuxie* @Create: 2026/2/4 0:29*/@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class RedisTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void test001(){//set / get//        stringRedisTemplate.opsForValue()
//                .set("test:key", "hello", 60, TimeUnit.SECONDS);
//
//        String value = stringRedisTemplate.opsForValue().get("test:key");
//        System.out.println("value = " + value);//        Boolean exists = stringRedisTemplate.hasKey("test:key");
//        System.out.println("exists = " + exists);
//        if (exists){
//            stringRedisTemplate.delete("test:key");
//
//        }//设置过期时间// stringRedisTemplate.expire("test:key", 20, TimeUnit.MINUTES);//查看剩余 TTLLong ttl = stringRedisTemplate.getExpire("test:key", TimeUnit.SECONDS);System.out.println("ttl = " + ttl);}
}

  配置文件

spring.redis.host=192.168.31.60spring.redis.port=6379spring.redis.password=spring.redis.database=0spring.redis.timeout=3000

  

image

 

image

 

image

image

 

package com.java;import lombok.extern.slf4j.Slf4j;
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;/*** @Description:* @Author: tutu-qiuxie* @Create: 2026/2/4 0:58*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class RedisUpTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void test002(){//自增 / 自减(限流、计数神器)
//        Long count = stringRedisTemplate.opsForValue()
//                .increment("login:count:user:1");
//        System.out.println("count = " + count);
//
//        Long increment = stringRedisTemplate.opsForValue()
//                .increment("money:balance", -10);
//        System.out.println("increment = " + increment);//Hash 存 / 取字段//        stringRedisTemplate.opsForHash()
//                .put("user:1", "name", "zhangsan");
//
//        Object name = stringRedisTemplate.opsForHash()
//                .get("user:1", "name");
//        System.out.println("name = " + name);//左进右出(队列)//        stringRedisTemplate.opsForList()
//                .leftPush("video:transcode:queue", "videoId=123");
//        stringRedisTemplate.opsForList()
//                .leftPush("video:transcode:queue", "videoId=1231");
//        stringRedisTemplate.opsForList()
//                .leftPush("video:transcode:queue", "videoId=1232");
//
//
//
//        String task;
//        while ((task = stringRedisTemplate.opsForList().rightPop("video:transcode:queue")) != null) {
//            try {
//                Thread.sleep(1000);
//                log.info("Processing task:{} ",task);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        System.out.println("All tasks processed.");//Set(去重 / 关系)//        stringRedisTemplate.opsForSet()
//                .add("video:likes:100", "user1", "user2");
//
//        Boolean liked = stringRedisTemplate.opsForSet()
//                .isMember("video:likes:100", "user1");
//        System.out.println("liked = " + liked);
//
//        Boolean likes = stringRedisTemplate.opsForSet()
//                .isMember("video:likes:100", "user2");
//        System.out.println("likes = " + likes);//统计数量Long count = stringRedisTemplate.opsForSet().size("video:likes:100");System.out.println("count = " + count);}}

  

 

http://www.jsqmd.com/news/339753/

相关文章:

  • 从BLIP到Deepbooru:揭秘AI图像标注背后的语言学博弈
  • MetaTube效能提升指南:解决媒体库管理难题的7个实战方案
  • AI读脸术JavaScript调用:前端直接集成可能性分析
  • 【网络攻防】ARP 欺骗深度解析:双向欺骗 + arpspoof,轻松达成 100% 断网操作!
  • Qwen3-4B-Instruct企业应用:技术文档自动生成与代码辅助开发
  • HG-ha/MTools一文详解:开源桌面AI套件在中小企业内容生产中的落地实践
  • MusePublic艺术创作引擎保姆级教程:从安装到生成首张高清人像
  • Spark代码规范指南:写出高性能Spark应用的最佳实践
  • LongCat-Image-Editn镜像免配置原理:预置Gradio+torch+transformers全栈依赖
  • Pi0具身智能v1网络通信:TCP/IP协议栈优化实践
  • EcomGPT电商大模型实测:一键生成精准商品分类与描述
  • RMBG-2.0参数详解:图像缩放至1024×1024原理与尺寸还原算法说明
  • Banana Vision Studio:10个隐藏技巧让你的设计更专业
  • 设计师福音:Banana Vision Studio平铺拆解图生成全攻略
  • PDF-Extract-Kit-1.0快速上手指南:Jupyter中可视化查看布局识别热力图
  • Swin2SR新手入门:5分钟学会图片无损放大
  • 从零到一:CentOS 7上MySQL与Python的深度集成实战
  • 开箱即用!Qwen2.5-1.5B本地智能助手效果展示
  • AI数字美容刀GPEN:拯救你的模糊自拍和合影
  • ollama部署embeddinggemma-300m:面向开发者的一站式多语言嵌入服务搭建指南
  • 开源大模型AnythingtoRealCharacters2511一文详解:LoRA微调原理与图像保真技巧
  • Qwen2.5-VL-7B-Instruct部署实操:24G显存极限压测与分辨率智能限控方案
  • DeepSeek-R1-Distill-Qwen-1.5B部署案例:高校AI通识课实验平台本地化部署
  • Chandra OCR企业应用:金融票据识别+表单复选框提取+JSON结构化入库案例
  • Vue+SpringBoot全栈开发中的数据库设计陷阱与突围
  • 从零开始:非专业人士如何用SNAP完成Sentinel影像镶嵌的实战指南
  • AI辅助诊断:MedGemma X-Ray系统部署与使用详解
  • 运维工程师必备:Hunyuan-MT 7B翻译服务监控与维护
  • AI读脸术前端集成:WebUI上传功能定制开发指南
  • 从零开始:灵毓秀-牧神-造相Z-Turbo文生图模型入门教程