spring-boot项目整合redis

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。 Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

1.项目pom中配置

<!--redis数据库-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.在项目的application.properties中配置

#redis数据库链接配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
  1. 使用StringRedisTemplate对象操作redis即可
@Service
public class RedisService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 执行set操作
     *
     * @param key
     * @return
     */
    public void set(final String key, final String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 执行get操作
     * @param key
     * @return
     */
    public String get(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 执行delete操作
     *
     * @param key
     * @return
     */
    public boolean del(final String key) {
        return stringRedisTemplate.delete(key);
    }


    /**
     * 执行set操作并且设置生存时间,单位为:秒
     *
     * @param key
     * @param value //TimeUnit.SECONDS 秒
     *              //TimeUnit.MINUTES 分
     * @return
     */
    public void set(final String key, final String value, final Integer seconds) {
        stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }


    /**
     * 执行hset操作
     *
     * @param key
     * @param
     * @return
     */
    public void hset(final String key, final String mapkey, final String mapvalue) {
        stringRedisTemplate.opsForHash().put(key, mapkey, mapvalue);
    }

    /**
     * 执行hgetAll操作
     *
     * @param key
     * @param
     * @return
     */
    public Map<String, String> hgetAll(final String key) {
        return (Map)stringRedisTemplate.opsForHash().entries(key);
    }

    /**
     * 执行hdel操作
     *
     * @param key
     * @param
     * @return
     */
    public long hdel(final String key, final String[] strings) {
        return stringRedisTemplate.opsForHash().delete(key, strings);
    }


    /**
     * 执行hkeys操作
     *
     * @param
     * @param key
     * @return
     */
    public Set<String> hkeys(final String key) {
        return (Set) stringRedisTemplate.opsForHash().keys(key);
    }


    /**
     * 执行hvalues操作
     *
     * @param
     * @param key
     * @return
     */
    public List<String> hvalues(final String key) {
        return (List) stringRedisTemplate.opsForHash().values(key);
    }


    /**
     * 执行hget操作
     *
     * @param
     * @param key
     * @return
     */
    public String hget(final String key, final String mapkey) {
        return (String)stringRedisTemplate.opsForHash().get(key, mapkey);
    }

    /**
     * 执行hmset操作
     *
     * @param
     * @param key
     * @return
     */
    public void hmset(final String key, final Map<String, String> mapvalue) {
        stringRedisTemplate.opsForHash().putAll(key, mapvalue);
    }


    /**
     * 执行lpush操作
     * @param
     * @param key
     * @return
     */
    public long lpush(final String key,final String value) {
      return stringRedisTemplate.opsForList().leftPush(key,value);
    }

    /**
     * 执行lpop操作
     * @param
     * @param key
     * @return
     */
    public String lpop(final String key) {
        return stringRedisTemplate.opsForList().leftPop(key);
    }

    /**
     * 执行rpop操作
     * @param
     * @param key
     * @return
     */
    public String rpop(final String key) {
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    /**
     * 执行list操作
     * 在列表中的尾部添加一个个值,返回列表的长度
     *
     * @param key
     * @return
     */
    public Long rpush(final String key, final String value) {
        return stringRedisTemplate.opsForList().rightPush(key,value);
    }

    /**
     * 执行list操作
     * 在列表中的尾部添加多个值,返回列表的长度
     *
     * @param key
     * @return
     */
    public Long rpush(final String key, final String[] value) {
        return stringRedisTemplate.opsForList().rightPushAll(key,value);
    }
    /**
     * 执行list操作
     * 获取List列表
     *
     * @param key
     * @return
     */
    public List<String> lrange(final String key, final long start, final long end) {
        return stringRedisTemplate.opsForList().range(key,start,end);
    }


    /**
     * 执行list操作
     * 通过索引获取列表中的元素
     *
     * @param key
     * @return
     */
    public String lindex(final String key, final long index) {
        return stringRedisTemplate.opsForList().index(key,index);
    }

    /**
     * 执行list操作
     * 获取列表长度,key为空时返回0
     *
     * @param key
     * @return
     */
    public Long llen(final String key) {
        return stringRedisTemplate.opsForList().size(key);
    }


    public boolean expire(final String key, final Integer seconds) {
        return stringRedisTemplate.expire(key,seconds,TimeUnit.SECONDS);
    }
}

4.如果有好的方式,可以留言沟通。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
53 0
|
2月前
|
NoSQL Java Redis
Spring Boot 监听 Redis Key 失效事件实现定时任务
Spring Boot 监听 Redis Key 失效事件实现定时任务
64 0
|
4天前
|
XML Java 数据格式
Spring 项目如何使用AOP
Spring 项目如何使用AOP
18 2
|
4天前
|
Java Spring
Spring boot项目如何发送邮件
Spring boot项目如何发送邮件
14 2
|
11天前
|
存储 NoSQL Java
Spring Boot与Redis:整合与实战
【4月更文挑战第29天】Redis,作为一个高性能的键值存储数据库,广泛应用于缓存、消息队列、会话存储等多种场景中。在Spring Boot应用中整合Redis可以显著提高数据处理的效率和应用的响应速度。
26 0
|
14天前
|
Java Maven Docker
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
26 2
|
17天前
|
XML NoSQL Java
spring整合redis出错
spring整合redis出错
16 0
|
21天前
|
Java Linux 虚拟化
Docker 部署spring-boot项目(超详细 包括Docker详解、Docker常用指令整理等)
Docker 部署spring-boot项目(超详细 包括Docker详解、Docker常用指令整理等)
56 1
|
22天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
29 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
22天前
|
存储 Java Maven
江帅帅:Spring Boot 底层级探索系列 01- 搭建项目
江帅帅:Spring Boot 底层级探索系列 01- 搭建项目
35 0
江帅帅:Spring Boot 底层级探索系列 01- 搭建项目