spring整合redis(常用数据类型操作)

1、字符串(String)操作

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisStringService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void setStringValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String getStringValue(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}

2、列表 (List) 操作

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisListService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void pushListValue(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value); // 右侧推入
    }

    public String popListValue(String key) {
        return (String) redisTemplate.opsForList().leftPop(key); // 左侧弹出
    }

    public List<Object> getListValues(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end); // 获取指定范围的列表元素
    }
}

3、集合 (Set) 操作

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisSetService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void addSetValue(String key, String value) {
        redisTemplate.opsForSet().add(key, value);
    }

    public Set<Object> getSetValues(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    public Long removeSetValue(String key, String value) {
        return redisTemplate.opsForSet().remove(key, value);
    }
}

4、有序集合 (Sorted Set) 操作

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisZSetService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void addZSetValue(String key, String value, double score) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    public Set<Object> getZSetValues(String key, long start, long end) {
        return redisTemplate.opsForZSet().range(key, start, end);
    }

    public Long removeZSetValue(String key, String value) {
        return redisTemplate.opsForZSet().remove(key, value);
    }
}

5、哈希 (Hash) 操作

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisHashService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void putHashValue(String key, String hashKey, String value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    public Object getHashValue(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    public Map<Object, Object> getAllHashValues(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public Long deleteHashField(String key, String hashKey) {
        return redisTemplate.opsForHash().delete(key, hashKey);
    }
}
相关推荐
IT毕设实战小研24 分钟前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
甄超锋2 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
鼠鼠我捏,要死了捏2 小时前
生产环境Redis缓存穿透与雪崩防护性能优化实战指南
redis·cache
Java小白程序员5 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
甄超锋6 小时前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
曾经的三心草6 小时前
微服务的编程测评系统11-jmeter-redis-竞赛列表
redis·jmeter·微服务
还是鼠鼠6 小时前
tlias智能学习辅助系统--Maven 高级-私服介绍与资源上传下载
java·spring boot·后端·spring·maven
努力努力再努力wz8 小时前
【c++深入系列】:万字详解模版(下)
java·c++·redis
还是大剑师兰特8 小时前
Spring面试题及详细答案 125道(1-15) -- 核心概念与基础1
spring·大剑师·spring面试题·spring教程
2301_793086878 小时前
Redis 04 Reactor
数据库·redis·缓存