scalacache 配合 guava

代码

Scala 复制代码
package com.yy.guava

import scalacache._
import scalacache.guava._
import scalacache._
import scalacache.guava._
import com.google.common.cache.CacheBuilder
import scalacache.modes.sync._
import scalacache.serialization.binary._
import scalacache._
import guava._
import memoization._

import com.google.common.cache.CacheBuilder

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import java.time.Instant

/**
 * scala套壳的框架. 支持 guava caffeine 作为底层实现.
 * https://cb372.github.io/scalacache/
 * https://cb372.github.io/scalacache/docs/cache-implementations.html
*/
object scalaCacheDemo1 {
  def main(args: Array[String]): Unit = {


    val underlyingGuavaCache = CacheBuilder
      .newBuilder()
      .maximumSize(1000L) // 配置缓存个数
      .build[String, Entry[String]]

    implicit val guavaCache: Cache[String] = GuavaCache(underlyingGuavaCache)

    val db = Map(1 -> "A", 2 -> "B", 3 -> "C")

    def queryDb(id: Int): String = {
      println(s"Getting id $id from db")
      db(id)
    }

    /*
    https://cb372.github.io/scalacache/docs/memoization.html
    配置缓存时长
     */
    def getUser(id: Int): String = memoizeSync(Option(1.seconds)) {
        queryDb(id)
    }


    println(getUser(1))
    Thread.sleep(100)

    println(getUser(1))
    Thread.sleep(2000)

    println(getUser(1))
    Thread.sleep(100)

    println(getUser(2))
    Thread.sleep(100)

    println(getUser(2))
    Thread.sleep(100)

    println(getUser(2))
    Thread.sleep(100)


    //  Output - Only hits 'db' once
    //  Getting id 1 from db
    //  Getting id 2 from db


  }

}

输出

Scala 复制代码
Getting id 1 from db
A
A
Getting id 1 from db
A
Getting id 2 from db
B
B
B

pom

XML 复制代码
       <!-- https://mvnrepository.com/artifact/com.github.cb372/scalacache-core -->
        <dependency>
            <groupId>com.github.cb372</groupId>
            <artifactId>scalacache-core_${scala.binary.version}</artifactId>
            <version>${scalacache.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.cb372/scalacache-guava -->
        <dependency>
            <groupId>com.github.cb372</groupId>
            <artifactId>scalacache-guava_${scala.binary.version}</artifactId>
            <version>${scalacache.version}</version>
        </dependency>
相关推荐
Wx-bishekaifayuan1 天前
springboot市社保局社保信息管理与分析系统-计算机设计毕业源码03479
java·css·spring boot·spring·spring cloud·servlet·guava
鹏阿鹏3 天前
【SpringBoot】Guava包Cache缓存的使用
spring boot·缓存·guava
Wx-bishekaifayuan7 天前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
非往9 天前
五、Java并发 Java Google Guava 实现
java·开发语言·guava
gorgor在码农9 天前
Google Guava 发布订阅模式/生产消费者模式 使用详情
guava
鱼跃鹰飞12 天前
大厂面试真题-如果使用guava limiter实现实例级别的缓存
缓存·面试·guava
鱼跃鹰飞14 天前
大厂面试真题-caffine比guava有什么提升?
数据库·mysql·面试·职场和发展·guava
ldxxxxll1 个月前
Guava使用指南2
java·算法·guava
电脑令人发狂的1 个月前
Spring Boot读取resources目录下文件(打成jar可用),并放入Guava缓存
spring boot·jar·guava
杏花春雨江南2 个月前
guava里常用功能
服务器·windows·guava