spring cache + redis整合

謹以此做個筆記。
平時我們使用的redis無非是用作緩存以及分布式session,其他的作用就視項目需求而定了富雅。
這里主要講講redis作為緩存的作用掸驱。
使用redis做緩存,可以選擇單獨使用没佑,也可以和spring cache整合毕贼。
單獨使用redis也很方便,只是代碼耦合性高了蛤奢,所以這次試試與spring cache整合使用鬼癣。
spring cache主要是代碼級的緩存,加上redis可以使代碼解耦又可以使緩存以單純的內存級發(fā)揮作用啤贩〈海可能理解不完全對,具體的可以再去深入了解一下痹屹,這里謹以此做個筆記而已章郁。
話不多說,開始痢掠。
spring boot版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
</parent>

1驱犹、引入redis和spring cache依賴包嘲恍,pom.xml文件如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

注:lombok依賴包可以自動生成getter足画、setter以及構造函數(shù)等方法,挺好用的佃牛。
2淹辞、創(chuàng)建一個簡單的POJO

@Data
public class User {
    private int id;
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、因為這里用的mybatis俘侠,所以創(chuàng)建與POJO對應的mapper

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO users(name, age) VALUES(#{name}, #{age})")
    int save(User user);

    @Select("SELECT * FROM users WHERE id = #{id}")
    User getById(int id);

    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteUser(int id);

    @Update("UPDATE users SET name = #{name}, age = #{age} WHERE id = #{id}")
    int updateUser(User user);
}

4象缀、創(chuàng)建UserService

public interface UserService {

    User getById(int id);

    int deleteUser(int id);

    int updateUser(int id, User user);

    void cleanCache();
}

5、配置application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 761341
  redis:
    host: localhost
    port: 6379
    pool:
      max-active: 8
      max-idle: 8
      max-wait: -1
      min-idle: 1
management:
  security:
    enabled: false
redis-expires:
  cache-names: redisCache
  default-expiration: 3600
  expires:
    redisCache: 3600

6爷速、配置RedisCacheManager

@Configuration
@EnableCaching
@EnableConfigurationProperties(RedisProperties.class)
@ConfigurationProperties(prefix = "redis-expires")
public class CustomRedisCacheManager {
    @Autowired
    private RedisProperties redisProperties;

    private List<String> cacheNames;
    private long defaultExpiration;
    private Map<String, Long> expires;

    public List<String> getCacheNames() {
        return cacheNames;
    }

    public void setCacheNames(List<String> cacheName) {
        this.cacheNames = cacheName;
    }

    public long getDefaultExpiration() {
        return defaultExpiration;
    }

    public void setDefaultExpiration(long defaultExpiration) {
        this.defaultExpiration = defaultExpiration;
    }

    public Map<String, Long> getExpires() {
        return expires;
    }

    public void setExpires(Map<String, Long> expires) {
        this.expires = expires;
    }

    /**
     * 重寫緩存key生成機制
     * @return
     */
//    @Bean
//    @Primary
//    public KeyGenerator keyGenerator() {
//        return (target, method, 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("redisCacheManager")
    @Primary
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setCacheNames(cacheNames);
        cacheManager.setDefaultExpiration(defaultExpiration);
        cacheManager.setExpires(expires); // 設置緩存過期時間
        cacheManager.setUsePrefix(true); // 是否使用緩存前綴
        redisTemplate.afterPropertiesSet();
        return cacheManager;
    }

    /**
      * 配置json序列化
      */
    @Bean
    @Primary
    public RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redisProperties.getHost());
        jedisConnectionFactory.setPort(redisProperties.getPort());
        jedisConnectionFactory.setTimeout(redisProperties.getTimeout());
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    private JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        RedisProperties.Pool props = this.redisProperties.getPool();
        jedisPoolConfig.setMaxTotal(props.getMaxActive());
        jedisPoolConfig.setMaxIdle(props.getMaxIdle());
        jedisPoolConfig.setMinIdle(props.getMinIdle());
        jedisPoolConfig.setMaxWaitMillis(props.getMaxWait());
        return jedisPoolConfig;
    }
}

注:KeyGenerator可以配置緩存key的生成策略央星,如果不配置將使用SimpleKeyGenerator生成key,源碼如下惫东,但是建議還是在處理緩存的時候指定key莉给。

public static Object generateKey(Object... params) {
    if (params.length == 0) {
        return SimpleKey.EMPTY;
    }
    if (params.length == 1) {
        Object param = params[0];
        if (param != null && !param.getClass().isArray()) {
            return param;
        }
    }
    return new SimpleKey(params);
}

7、實現(xiàn)UserService

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    //這里指定了cacheNames = "redisCache"表示使用我們配置的默認緩存管理器也就是RedisCacheManager廉沮。debug模式可以看見第二次查詢時不會進入方法體颓遏。
    @Cacheable(cacheNames = "redisCache", key = "#id")
    public User getById(int id) {
        return userMapper.getById(id);
    }

    @Override
    @CacheEvict(cacheNames = "redisCache", key = "#id")
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    //更新指定緩存
    @CachePut(cacheNames = "redisCache", key = "#id")
    //清除指定緩存
    //@CacheEvict(cacheNames = "redisCache", key = "#id")
    public int updateUser(int id, User user) {
        user.setId(id);
        return userMapper.updateUser(user);
    }

    @Override
    //allEntries = true:清除所有緩存
    @CacheEvict(cacheNames = "redisCache", allEntries = true)
    public void cleanCache() {

    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市滞时,隨后出現(xiàn)的幾起案子叁幢,更是在濱河造成了極大的恐慌,老刑警劉巖坪稽,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曼玩,死亡現(xiàn)場離奇詭異鳞骤,居然都是意外死亡,警方通過查閱死者的電腦和手機黍判,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門弟孟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人样悟,你說我怎么就攤上這事拂募。” “怎么了窟她?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵陈症,是天一觀的道長。 經(jīng)常有香客問我震糖,道長录肯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任吊说,我火速辦了婚禮论咏,結果婚禮上,老公的妹妹穿的比我還像新娘颁井。我一直安慰自己厅贪,他們只是感情好,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布雅宾。 她就那樣靜靜地躺著养涮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪眉抬。 梳的紋絲不亂的頭發(fā)上贯吓,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音蜀变,去河邊找鬼悄谐。 笑死,一個胖子當著我的面吹牛库北,可吹牛的內容都是我干的爬舰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼贤惯,長吁一口氣:“原來是場噩夢啊……” “哼洼专!你這毒婦竟也來了?” 一聲冷哼從身側響起孵构,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤屁商,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜡镶,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡雾袱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了官还。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片芹橡。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖望伦,靈堂內的尸體忽然破棺而出林说,到底是詐尸還是另有隱情,我是刑警寧澤屯伞,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布腿箩,位于F島的核電站,受9級特大地震影響劣摇,放射性物質發(fā)生泄漏珠移。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一末融、第九天 我趴在偏房一處隱蔽的房頂上張望钧惧。 院中可真熱鬧,春花似錦勾习、人聲如沸浓瞪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽追逮。三九已至,卻和暖如春粹舵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背骂倘。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工眼滤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人历涝。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓诅需,卻偏偏與公主長得像,于是被迫代替她去往敵國和親荧库。 傳聞我的和親對象是個殘疾皇子堰塌,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn)分衫,斷路器场刑,智...
    卡卡羅2017閱讀 134,654評論 18 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,808評論 6 342
  • application的配置屬性。 這些屬性是否生效取決于對應的組件是否聲明為Spring應用程序上下文里的Bea...
    新簽名閱讀 5,367評論 1 27
  • 這些屬性是否生效取決于對應的組件是否聲明為 Spring 應用程序上下文里的 Bean(基本是自動配置的)蚪战,為一個...
    發(fā)光的魚閱讀 1,424評論 0 14
  • 一頓聚餐下來牵现,我發(fā)現(xiàn)自己不適合這種場面铐懊。瞬間體會到了當時陶淵明不為五斗米而折腰的骨氣。如果自己是陶淵明瞎疼,肯定...
    公子小卜閱讀 1,299評論 2 2