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);
    }
}
相关推荐
武子康4 小时前
Java-71 深入浅出 RPC Dubbo 上手 父工程配置编写 附详细POM与代码
java·分布式·程序人生·spring·微服务·rpc·dubbo
椰椰椰耶7 小时前
【Spring】拦截器详解
java·后端·spring
笑衬人心。14 小时前
Spring的`@Value`注解使用详细说明
java·后端·spring
金心靖晨15 小时前
redis汇总笔记
数据库·redis·笔记
Hello.Reader15 小时前
Redis性能基准测试
数据库·redis·junit
如果'\'真能转义说15 小时前
Docker Desktop 挂载本地Win系统配置指南:Redis/MySQL/RabbitMQ持久化与自启设置
docker·容器·bootstrap·docker desktop
沃夫上校16 小时前
Spring Boot 中使用 Redis
spring boot·redis
即将进化成人机16 小时前
Spring-----MVC配置和基本原理
java·spring·mvc
老神在在00116 小时前
SpringMVC2
java·前端·学习·spring·java-ee
老神在在00116 小时前
SpringMVC3
java·前端·学习·spring·java-ee