美文网首页spring-boot 踩坑程序员
spring-boot-cache应用小结(Redis篇)

spring-boot-cache应用小结(Redis篇)

作者: aef5bc56a01e | 来源:发表于2019-03-14 16:36 被阅读1次

背景

最近公司项目处于停滞期,私想着是时候优化代码了,于是想到了缓存的应用。于是遇到了一系列的问题,在此稍作总结。
之前自己用redis存东西的时候,都是手动操作,略显麻烦,于是就想着用一下spring-cache。

spring-boot-cache

spring cache支持天然支持多种缓存

  • GENERIC
  • JCache
  • EhCache
  • HAZELCAST
  • INFINISPAN
  • COUCHBASE
  • REDIS
  • CAFFEINE
  • SIMPLE
    具体都是哪些,本文不一一讨论,自行Google(因为我只用了redis 😂)

相关注解

  • EnableCaching 开启注解
  • CacheConfig 类注解 统一配置该类中所有要缓存的数据
  • Cacheable 方法注解 如果从缓存中取到了对应的数据,则直接返回;否则执行方法内的代码,并将返回的数据存入缓存中
  • CachePut 方法注解 无论如何都会执行方法内的代码,并将返回的数据存入缓存中
  • CacheEvict 方法注解 无论如何都会执行方法内的代码,并将返回的数据从缓存中删除
    详细用法可参看spring官方文档, 或者这篇博客

配置

spring:
  cache:
    type: redis  #要用的缓存类型
    cache-names: test #默认的缓存名称,如果在对应的cacheName不指定,则会用到该名字
    redis:
      key-prefix:  TEST_ #缓存key的前缀,如果不配置,则会根据cacheNames和key组合为redis的key
      time-to-live:  10m #缓存时间

Talk is cheap, show me code.

import java.io.Serializable
import javax.persistence.Entity
import javax.persistence.Id
import org.springframework.cache.annotation.Cacheable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory

inline fun <reified T> logger(): Logger {
    return LoggerFactory.getLogger(T::class.java)
}

@Entity
data class SysConfig(
        @Id
        var id: Long = 0,
        var key: String? = null,
        var value: String? = null,
        var remark: String? = null,
        var status: Int = 0
): Serializable

interface SysConfigRepository : JpaRepository<SysConfig, Long> {
    fun findByKey(key: String): SysConfig?
}

@Service
class SysConfigService(val configRepository: SysConfigRepository) {
    val log = logger<SysConfigService>()
    @Cacheable("SYS_CONFIG_DETAIL", key = "#name + '_' + #i")
    fun findByName(name: String, i: Int): SysConfig? {
        log.info("load from db {} {}", name, i)
        return configRepository.findByKey(name)
    }

    @Cacheable("SYS_CONFIG_ALL")
    fun all(): List<SysConfig> {
        log.info("load from db")
        return configRepository.findAll()
    }
}

@RestController
@RequestMapping("config")
class SysConfigController(val sysConfigService: SysConfigService) {

    @GetMapping("/{name}")
    fun config(@PathVariable("name") name: String,
               @RequestParam(value = "i", required = false, defaultValue = "0")i: Int) = sysConfigService.findByName(name, i)

    @GetMapping("")
    fun config() = sysConfigService.all()
}

访问对应的url即可去redis中观察

image.png

PS:如果有缓存hash需求的可参看我的另外一篇文章《spring-boot-cache之 lettuce VS redisson》

相关文章

  • spring-boot-cache之 lettuce VS re

    背景 接上篇《spring-boot-cache应用小结(Redis篇)》由于有的项目用到了redisson,想着...

  • spring-boot-cache应用小结(Redis篇)

    背景 最近公司项目处于停滞期,私想着是时候优化代码了,于是想到了缓存的应用。于是遇到了一系列的问题,在此稍作总结。...

  • Redis应用-布隆过滤器

    系列文章Redis应用-分布式锁Redis应用-异步消息队列与延时队列Redis应用-位图Redis应用-Hype...

  • Redis应用-Geo

    系列文章Redis应用-分布式锁Redis应用-异步消息队列与延时队列Redis应用-位图Redis应用-Hype...

  • Redis Learning

    应用场景:redis 适用于小而热的数据 Redis应用场景 Redis作者谈Redis应用场景我们在应用redi...

  • 淘宝技术架构文档:Redis+多线程+kafka+Nginx+数

    Redis实战核心篇 第1章 初识Redis 第2章 使用Redis构建Web应用 第二部分核心概念 第3章 Re...

  • redis数据结构

    Redis各种数据结构在我们项目中都有运用,前面写了一篇redis的常见应用场景,下面就redis的数据结构做了一...

  • 云Hbase数据库在亿方云实践之路.pdf

    第二篇:HBase在大搜车金融业务中的应用实践.pdf 关于缓存Redis Codis 集群演化与 Redis 异...

  • Redis缓存总结

    Redis 原理及应用(1)--数据类型及底层实现方式redis学习(八)——redis应用场景 --不错哦 Re...

  • redis 小结

    1 redis概述 1.1 什么是redis   Redis的全称是REmote Dictionary Serve...

网友评论

    本文标题:spring-boot-cache应用小结(Redis篇)

    本文链接:https://www.haomeiwen.com/subject/lmtcmqtx.html