spring boot 使用 redis 缓存

spring boot redis 使用 1

Redis:
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave模式的数据备份。

spring boot 中如何使用:

  1. 引入依赖 pom.xml

    <!-- redis -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. 配置redis

    #redis配置
    #Redis服务器地址
    spring.redis.host=192.168.5.10
    #Redis服务器连接端口
    spring.redis.port=6379
    #Redis数据库索引(默认为0)
    spring.redis.database=4
    #连接池最大连接数(使用负值表示没有限制)
    spring.redis.jedis.pool.max-active=50
    #连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.jedis.pool.max-wait=3000
    #连接池中的最大空闲连接
    spring.redis.jedis.pool.max-idle=20
    #连接池中的最小空闲连接
    spring.redis.jedis.pool.min-idle=2
    #连接超时时间(毫秒)
    spring.redis.timeout=5000

  3. 修改启动类 添加 @EnableCaching 注解

    @RestControllerbr/>@SpringBootApplication
    @MapperScan("com.example.demo.dao.mybatis")br/>@EnableCaching
    public class DemoApplication

  4. 实体类添加 序列化支持

    public class User implements Serializable

  5. 在 service 上加上缓存注解
    @Service
    public class MybatisUserService {
    
            @Autowired
            UserMapper userDao;
    
            public int add(User user){
                return   userDao.add(user);
            }
    
            public int update(User user){
                    return   userDao.update(user);
            }
    
            @Cacheable(value = "user-key")
            public User getById(long id){
                    System.out.println("从数据库获取数据");
                    return   userDao.findUserById(id);
            }
    }

----------------自动加入缓存结束------------
手动操作缓存:

@Component
public class RedisUtil {

        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        /**
         * 读取缓存
         *
         * @param key
         * @return
         */
        public String get(final String key) {
                return redisTemplate.opsForValue().get(key);
        }

        /**
         * 写入缓存
         */
        public boolean set(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().set(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 更新缓存
         */
        public boolean getAndSet(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().getAndSet(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 删除缓存
         */
        public boolean delete(final String key) {
                boolean result = false;
                try {
                        redisTemplate.delete(key);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }
}

redis 简单的操作介绍完成

spring boot 使用 redis 缓存

原文地址:https://blog.51cto.com/5013162/2404306

时间: 2024-07-31 00:45:33

spring boot 使用 redis 缓存的相关文章

Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 package springboot01cache.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; im

spring boot集成redis缓存

spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.3.RELEASE</version> </depen

spring boot 使用redis缓存

感谢大神分享! https://www.cnblogs.com/gdpuzxs/p/7222309.html (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> (2)修改项目启动类,增加注解@

Spring Boot使用redis实现数据缓存

基于Spring Boot 1.5.2.RELEASE版本,一方面验证与Redis的集成方法,另外了解使用方法. 集成方法 配置依赖 修改pom.xml,增加如下内容. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 配置Redis

SpringBoot(三) :Spring boot 中 Redis 的使用

前言: 这一篇讲的是Spring Boot中Redis的运用,之前没有在项目中用过Redis,所以没有太大的感觉,以后可能需要回头再来仔细看看. 原文出处: 纯洁的微笑 SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类数据库的特性,比如事务,HA,

Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

15套java架构师.集群.高可用.高可扩展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布式项目实战视频教程 视频课程内容包含: 高级Java架构师包含:Spring boot.Spring  cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat.Spring.MongoDB.ZeroMQ.Git.Nosql.Jvm.Mecached.Netty.Nio.Mina.性能调优.高并发.to

【redis】spring boot利用redis的Keyspace Notifications实现消息通知

前言 需求:当redis中的某个key失效的时候,把失效时的value写入数据库. github: https://github.com/vergilyn/RedisSamples 1.修改redis.conf 安装的redis服务默认是: notify-keyspace-events "",修改成 notify-keyspace-events Ex; 位置:redis安装目下的redis.windows-service.conf 或 redis.windows.conf.(具体看re

Spring Boot 入门之缓存和 NoSQL 篇(四)

原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多数请求都是在重复的获取相同的数据,如果使用缓存,将结果数据放入其中可以很大程度上减轻数据库的负担,提升系统的响应速度. 本篇将介绍 Spring Boot 中缓存和 NoSQL 的使用.上篇文章<Spring Boot 入门之持久层篇(三)>. 二.整合缓存 Spring Boot 针对不同的缓存

Spring Boot学习笔记——Spring Boot与Redis的集成

一.添加Redis缓存 1.添加Redis起步依赖 在pom.xml中添加Spring Boot支持Redis的依赖配置,具体如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </