最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot使用redis實現(xiàn)數(shù)據(jù)緩存

 更新時間:2017年04月17日 08:27:24   作者:小南家的青蛙  
本篇文章主要介紹了詳解Spring Boot使用redis實現(xiàn)數(shù)據(jù)緩存,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

基于spring Boot 1.5.2.RELEASE版本,一方面驗證與Redis的集成方法,另外了解使用方法。

集成方法

1、配置依賴

修改pom.xml,增加如下內(nèi)容。

  <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-redis</artifactId> 
  </dependency> 

2、配置Redis

修改application.yml,增加如下內(nèi)容。

spring:
  redis:
    host: localhost 
    port: 6379
    pool:
      max-idle: 8 
      min-idle: 0
      max-active: 8
      max-wait: -1

3、配置Redis緩存

package net.jackieathome.cache;

import java.lang.reflect.Method;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching // 啟用緩存特性
public class RedisConfig extends CachingConfigurerSupport {
  // 緩存數(shù)據(jù)時Key的生成器,可以依據(jù)業(yè)務(wù)和技術(shù)場景自行定制
// @Bean
// public KeyGenerator customizedKeyGenerator() {
//   return new KeyGenerator() {
//     @Override
//     public Object generate(Object target, Method method, Object... params) {
//       StringBuilder sb = new StringBuilder();
//       sb.append(target.getClass().getName());
//       sb.append(method.getName());
//       for (Object obj : params) {
//         sb.append(obj.toString());
//       }
//       return sb.toString();
//     }
//   };
//
// }
  // 定制緩存管理器的屬性,默認提供的CacheManager對象可能不能滿足需要
  // 因此建議依賴業(yè)務(wù)和技術(shù)上的需求,自行做一些擴展和定制
  @Bean
  public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
    redisCacheManager.setDefaultExpiration(300);
    return redisCacheManager;
  }

  @Bean
  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
  }
}

驗證集成后的效果

考慮到未來參與的項目基于MyBatis實現(xiàn)數(shù)據(jù)庫訪問,而利用緩存,可有效改善Web頁面的交互體驗,因此設(shè)計了如下兩個驗證方案。

方案一

在訪問數(shù)據(jù)庫的數(shù)據(jù)對象上增加緩存注解,定義緩存策略。從測試效果看,緩存有效。

1、頁面控制器

package net.jackieathome.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.jackieathome.bean.User;
import net.jackieathome.dao.UserDao;
import net.jackieathome.db.mapper.UserMapper;

@RestController
public class UserController {

  @Autowired
  private UserDao userDao;

  @RequestMapping(method = RequestMethod.GET, value = "/user/id/{id}")
  public User findUserById(@PathVariable("id") String id) {
    return userDao.findUserById(id);
  }

  @RequestMapping(method = RequestMethod.GET, value = "/user/create")
  public User createUser() {
    long time = System.currentTimeMillis() / 1000;

    String id = "id" + time;
    User user = new User();
    user.setId(id);
    userDao.createUser(user);

    return userDao.findUserById(id);
  }
}

2、Mapper定義

package net.jackieathome.db.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import net.jackieathome.bean.User;


@Mapper
public interface UserMapper {

  void createUser(User user);

  User findUserById(@Param("id") String id);
}

3、數(shù)據(jù)訪問對象

package net.jackieathome.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import net.jackieathome.bean.User;
import net.jackieathome.db.mapper.UserMapper;

@Component
@CacheConfig(cacheNames = "users")
@Transactional
public class UserDao {
  private static final Logger LOG = LoggerFactory.getLogger(UserDao.class);
  @Autowired
  private UserMapper userMapper;

  @CachePut(key = "#p0.id")
  public void createUser(User user) {
    userMapper.createUser(user);
    LOG.debug("create user=" + user);
  }

  @Cacheable(key = "#p0")
  public User findUserById(@Param("id") String id) {
    LOG.debug("find user=" + id);
    return userMapper.findUserById(id);
  }
}

方案二

直接在Mapper定義上增加緩存注解,控制緩存策略。從測試效果看,緩存有效,相比于方案一,測試代碼更加簡潔一些。

1、頁面控制器

package net.jackieathome.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.jackieathome.bean.User;
import net.jackieathome.dao.UserDao;
import net.jackieathome.db.mapper.UserMapper;

@RestController
public class UserController {

  @Autowired
  private UserMapper userMapper;

  @RequestMapping(method = RequestMethod.GET, value = "/user/id/{id}")
  public User findUserById(@PathVariable("id") String id) {
    return userMapper.findUserById(id);
  }

  @RequestMapping(method = RequestMethod.GET, value = "/user/create")
  public User createUser() {
    long time = System.currentTimeMillis() / 1000;

    String id = "id" + time;
    User user = new User();
    user.setId(id);
    userMapper.createUser(user);

    return userMapper.findUserById(id);
  }
}

2、Mapper定義

package net.jackieathome.db.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import net.jackieathome.bean.User;

@CacheConfig(cacheNames = "users")
@Mapper
public interface UserMapper {

  @CachePut(key = "#p0.id")
  void createUser(User user);

  @Cacheable(key = "#p0")
  User findUserById(@Param("id") String id);
}

總結(jié)

上述兩個測試方案并沒有優(yōu)劣之分,僅是為了驗證緩存的使用方法,體現(xiàn)了不同的控制粒度,在實際的項目開發(fā)過程中,需要依據(jù)實際情況做不同的決斷。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java并發(fā)編程中的volatile關(guān)鍵字詳解

    Java并發(fā)編程中的volatile關(guān)鍵字詳解

    這篇文章主要介紹了Java并發(fā)編程中的volatile關(guān)鍵字詳解,volatile?用于保證我們某個變量的可見性,使其一直存放在主存中,不被移動到某個線程的私有工作內(nèi)存中,需要的朋友可以參考下
    2023-08-08
  • SpringBoot導出Excel的四種方式小結(jié)

    SpringBoot導出Excel的四種方式小結(jié)

    近期接到了一個小需求,要將系統(tǒng)中的數(shù)據(jù)導出為Excel,且能將Excel數(shù)據(jù)導入到系統(tǒng),對于大多數(shù)研發(fā)人員來說,這算是一個最基本的操作了,本文就給大家總結(jié)一下SpringBoot導出Excel的四種實現(xiàn)方式,需要的朋友可以參考下
    2024-12-12
  • Mybatis實現(xiàn)聯(lián)表查詢并且分頁功能

    Mybatis實現(xiàn)聯(lián)表查詢并且分頁功能

    這篇文章主要介紹了Mybatis實現(xiàn)聯(lián)表查詢并且分頁功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Spring中網(wǎng)絡(luò)請求客戶端WebClient的使用詳解

    Spring中網(wǎng)絡(luò)請求客戶端WebClient的使用詳解

    作為替代,Spring 官方已在 Spring 5 中引入了 WebClient 作為非阻塞式 Reactive HTTP 客戶端,本文將通過樣例演示如何使用 WebClient,希望對大家有所幫助
    2024-04-04
  • SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏

    SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏

    這篇文章給大家介紹了三步實現(xiàn)SpringBoot全局日志記錄,整合Echarts實現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-03-03
  • springboot中RabbitMQ死信隊列的實現(xiàn)示例

    springboot中RabbitMQ死信隊列的實現(xiàn)示例

    死信隊列是一種特殊的消息隊列,用來存儲無法被正常消費的消息,常被用來實現(xiàn)延遲處理,異常消息處理等,本文主要介紹了springboot中RabbitMQ死信隊列的實現(xiàn)示例,感興趣的可以了解一下
    2024-01-01
  • 詳解Java高級特性之反射

    詳解Java高級特性之反射

    這篇文章主要介紹了Java高級特性之反射的相關(guān)知識,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • Java中數(shù)組的定義與使用

    Java中數(shù)組的定義與使用

    下面小編就為大家?guī)硪黄猨ava中數(shù)組的定義與使用小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • Java使用多線程處理未知任務(wù)數(shù)的方案介紹

    Java使用多線程處理未知任務(wù)數(shù)的方案介紹

    這篇文章主要為大家詳細介紹了Java如何使用多線程實現(xiàn)處理未知任務(wù)數(shù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-03-03
  • JVM GC 垃圾收集梳理總結(jié)

    JVM GC 垃圾收集梳理總結(jié)

    這篇文章主要介紹了JVM GC 垃圾收集梳理總結(jié),GC是一種自動的存儲管理機制。當一些被占用的內(nèi)存不再需要時,就應該予以釋放,這種存儲資源管理,稱為垃圾回收
    2022-07-07

最新評論

香港| 灵宝市| 景泰县| 灯塔市| 怀化市| 米泉市| 马山县| 东乡族自治县| 依兰县| 道孚县| 安图县| 潢川县| 安康市| 任丘市| 萝北县| 永年县| 石嘴山市| 班戈县| 沅江市| 东阿县| 洞口县| 启东市| 玉龙| 江陵县| 平原县| 饶河县| 微博| 玉田县| 谷城县| 大城县| 公安县| 临颍县| 达日县| 巢湖市| 道孚县| 米泉市| 凤翔县| 武夷山市| 麦盖提县| 榆社县| 新蔡县|