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);
    }
}
相关推荐
Kerwin要坚持日更10 分钟前
一文讲解Redis中的主从复制
数据库·redis·缓存
Suk-god11 分钟前
【Redis】基础知识入门
数据库·redis·缓存
2501_9032386536 分钟前
Spring MVC中环境配置的实战应用
java·spring·mvc·个人开发
程序员侠客行38 分钟前
Spring事务原理详解 三
java·后端·spring·架构
zfj3211 小时前
手动搭建Redis1主2从+ 3 Sentinel 高可用集群
redis·sentinel·高可用
m0_748230942 小时前
Redis 通用命令
前端·redis·bootstrap
m0_748247552 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
计算机-秋大田4 小时前
基于Spring Boot的农产品智慧物流系统设计与实现(LW+源码+讲解)
java·开发语言·spring boot·后端·spring·课程设计
Struggle Sheep4 小时前
linux安装redis
linux·运维·redis
计算机毕设指导64 小时前
基于SpringBoot的城乡商城协作系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven