StringRedisTemplate 常用方法
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; @RestController @Api(tags = "TestController") @RequestMapping("/test") @Slf4j public class TestController { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; @ApiOperation("test") @RequestMapping(value = "/test", method = RequestMethod.GET) public void test(String key, String hashkey, String value) { log.info("key:{},hashkey:{},value:{}", key, hashkey, value); { stringRedisTemplate.opsForValue().set(key, value); stringRedisTemplate.opsForValue().setIfAbsent(key, value); stringRedisTemplate.opsForValue().increment(key, 2); String value_out = stringRedisTemplate.opsForValue().get(key); log.info("value_out:{}", value_out); Boolean hasKey = stringRedisTemplate.hasKey(key);//判断是否有key所对应的值,有则返回true,没有则返回false Boolean expire = stringRedisTemplate.expire(key, 60, TimeUnit.SECONDS); // Boolean expireAt = stringRedisTemplate.expireAt(key, new Date()); byte[] dump = stringRedisTemplate.dump(key); Boolean delete = stringRedisTemplate.delete(key); } {//集合 Long add = stringRedisTemplate.opsForSet().add(key, value, value + "1", value + "2"); Long remove = stringRedisTemplate.opsForSet().remove(key, value); Long size = stringRedisTemplate.opsForSet().size(key); Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, value); Set<String> members = stringRedisTemplate.opsForSet().members(key); stringRedisTemplate.delete(key); } {//有序集合 stringRedisTemplate.opsForZSet().add(key, value, 0); stringRedisTemplate.opsForZSet().add(key, value + 1, 1); stringRedisTemplate.opsForZSet().add(key, value + 2, 2); Double score = stringRedisTemplate.opsForZSet().score(key, value); Set<String> rangeByScore = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, 2); log.info("rangeByScore:{}", rangeByScore); stringRedisTemplate.delete(key); } {//哈希 stringRedisTemplate.opsForHash().put(key, hashkey, value); stringRedisTemplate.opsForHash().putIfAbsent(key, hashkey, value); Map<String, String> stringStringHashMap = new HashMap<>(); stringStringHashMap.put("1", "2"); stringStringHashMap.put("3", "4"); stringRedisTemplate.opsForHash().putAll(key, stringStringHashMap); stringRedisTemplate.opsForHash().increment(key, hashkey, 2); stringRedisTemplate.opsForHash().hasKey(key, hashkey); Object o = stringRedisTemplate.opsForHash().get(key, hashkey); log.info("o:{}", o); Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key); log.info("entries:{}", entries); Set<Object> keys = stringRedisTemplate.opsForHash().keys(key); log.info("keys:{}", keys); List<Object> values = stringRedisTemplate.opsForHash().values(key); log.info("values:{}", values); stringRedisTemplate.delete(key); } {//列表 stringRedisTemplate.opsForList().leftPush(key, value); stringRedisTemplate.opsForList().leftPushIfPresent(key, value); stringRedisTemplate.opsForList().leftPush(key, value, value);//如果pivot处值存在则在pivot前面添加 stringRedisTemplate.opsForList().leftPushAll(key, value, value + 1, value + 2); stringRedisTemplate.opsForList().rightPush(key, value); stringRedisTemplate.opsForList().set(key, 0, value);//设置指定索引处元素的值 String index = stringRedisTemplate.opsForList().index(key, 0); log.info("index:{}", index); List<String> range = stringRedisTemplate.opsForList().range(key, 0, 2); log.info("range:{}", range); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2020-07-15 Shell排序
2019-07-15 Spring Cloud Hystrix Dashboard的使用 5.1.3
2019-07-15 Spring Cloud Hystrix 服务容错保护 5.1