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);
    }
}
相关推荐
Seven972 小时前
剑指offer-31、整数中1出现的次数
redis
AAA修煤气灶刘哥2 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
christine-rr2 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
青衫客362 天前
Spring异步编程- 浅谈 Reactor 核心操作符
java·spring·响应式编程
凯子坚持 c2 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
weixin_456904272 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件
波波烤鸭2 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
Cyan_RA92 天前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc