激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring Boot使用redis實現數據緩存

詳解Spring Boot使用redis實現數據緩存

2020-09-12 15:32小南家的青蛙 Java教程

本篇文章主要介紹了詳解Spring Boot使用redis實現數據緩存,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

集成方法

1、配置依賴

修改pom.xml,增加如下內容。

?
1
2
3
4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、配置Redis

修改application.yml,增加如下內容。

?
1
2
3
4
5
6
7
8
9
spring:
  redis:
    host: localhost
    port: 6379
    pool:
      max-idle: 8
      min-idle: 0
      max-active: 8
      max-wait: -1

3、配置Redis緩存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 {
  // 緩存數據時Key的生成器,可以依據業務和技術場景自行定制
// @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對象可能不能滿足需要
  // 因此建議依賴業務和技術上的需求,自行做一些擴展和定制
  @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實現數據庫訪問,而利用緩存,可有效改善Web頁面的交互體驗,因此設計了如下兩個驗證方案。

方案一

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

1、頁面控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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定義

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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、數據訪問對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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、頁面控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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定義

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}

總結

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/jackie_xiaonan/article/details/70187752

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久国产精麻豆96堂 | 国产一区二区三区四区五区精品 | 日本黄色免费片 | 末成年女av片一区二区 | 日本一区二区三区精品 | 女人解衣喂奶电影 | 国产午夜精品在线 | 不卡国产一区二区三区四区 | 午夜精品毛片 | 久久国产精品一区 | 渔夫荒淫艳史 | 大学生一级毛片 | 爽毛片 | 日本s级毛片免费观看 | 色骚综合 | 国产在线精品一区二区不卡 | 曰批全过程40分钟免费视频多人 | 性欧美videos 另类喷潮 | 日本高清电影在线播放 | 一区二区三区在线视频观看58 | 日本精品婷婷久久爽一下 | 成品片a免费直接观看 | 色999中文字幕 | 国产精品视频久久久 | 国内精品免费一区二区2001 | 欧美福利视频一区二区 | 国产乱淫av片免费观看 | 午夜视频久久 | 素人视频免费观看 | 羞羞视频在线免费 | 91看片在线播放 | 国产一区二区三区视频免费 | 欧美一级高潮片免费的 | 4p一女两男做爰在线观看 | 国产一区二区三区在线视频 | 多人乱大交xxxxx变态 | 色女人在线 | 久久成人综合视频 | 黄色一级毛片免费看 | 成人mm视频在线观看 | 亚洲午夜精选 |