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 简单的操作介绍完成

猜你喜欢

转载自blog.51cto.com/5013162/2404306