使用 SpringBoot 之 JPA 整合 Redis 實現緩衝

來源:互聯網
上載者:User
一、前言

資料庫中的 select 是使用最頻繁的,而且每次基本都是一樣的,而 update、delete、insert 使用的頻率沒有 select 高,並且每次基本都是不一樣的。為了減少資料庫的壓力,有必要對 select 使用緩衝,以前使用的是 Ehcache 做緩衝,但是其有一個很明顯的缺點,就是沒有 ip、port,而是使用路徑的,不容易起到共用快取的作用。故使用 Redis 做緩衝可以說是最佳的選擇了。 二、代碼

github 下載本案例代碼:https://github.com/larger5/SpringBootRedis.git
1、entity

package com.cun.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;/** * 使用者實體 * @author linhongcun * */@Entity@Table(name = "user")public class User{    @Id    @GeneratedValue    private Integer id;    @Column(length = 20)    private String userName;    @Column(length = 20)    private String password;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}
2、dao 介面
package com.cun.dao;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;import com.cun.entity.User;/** * 使用者 dao 介面 * @author linhongcun * */public interface UserDao extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{}
3、Service 介面
package com.cun.service;import java.util.List;import com.cun.entity.User;public interface UserService {    /**     * 擷取所有使用者     * @return     */    List<User> getAllUsers();}
4、Service 實作類別
package com.cun.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.cun.dao.UserDao;import com.cun.entity.User;import com.cun.service.UserService;@Service@CacheConfig(cacheNames = "userService")public class UserServiceImpl implements UserService {    @Autowired    private UserDao userDao;    /**     * 2、在 Service 層的實作類別中的方法@緩衝     * ① 指定緩衝的 key,為 wiselyKeyGenerator 的 bean     *      */    @Override    @Cacheable(value = "getAllUsers",keyGenerator="wiselyKeyGenerator")     public List<User> getAllUsers() {        return userDao.findAll();    }}
5、Controller
package com.cun.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import com.cun.entity.User;import com.cun.service.UserService;@RestControllerpublic class UserController {    @Autowired    private UserService userService;    @GetMapping("/all")    public List<User> getAllUsers() {        System.out.println("只有第一次才會列印sql語句");        return userService.getAllUsers();    }}
6、Redis 配置
package com.cun.conf;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;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 java.lang.reflect.Method;/** * Redis 緩衝配置類(通用) * @author linhongcun * */@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport {    /**     * 緩衝對象集合中,緩衝是以 key-value 形式儲存的。當不指定緩衝的 key 時,SpringBoot 會使用 SimpleKeyGenerator 產生 key。     * @return     */    @Bean    public KeyGenerator wiselyKeyGenerator() {        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();            }        };    }    @Bean    public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {        return new RedisCacheManager(redisTemplate);    }    @Bean    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {        StringRedisTemplate template = new StringRedisTemplate(factory);        @SuppressWarnings({ "rawtypes", "unchecked" })        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;    }}
7、application.yml
server:  port: 80  context-path: /spring:  redis:    host: 120.79.197.130    port: 6378  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/mybatis    username: root    password: 123  jpa:    hibernate:      ddl-auto: update    show-sql: true
8、Redis-SpringBoot 的 pom.xml

三、測試

1、原Redis資料庫,空空如也

2、第一次執行查詢

①瀏覽器

②控制台

③Redis 資料庫

2、第二次查詢

①控制台

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.