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>
相关推荐
二月夜16 天前
告别繁琐的比较器:掌握 Google Guava 的 Ordering 工具类
guava
二月夜16 天前
深入理解 Guava 新集合类型:超越 JDK 的数据结构利器
guava
文艺倾年18 天前
【源码精讲+简历包装】LeetcodeRunner—手搓调试器轮子(20W字-上)
java·jvm·人工智能·tomcat·编辑器·guava
shuair1 个月前
guava布隆过滤器及cuckoo过滤器
redis·guava
廋到被风吹走1 个月前
【缓存优化】缓存穿透:布隆过滤器(Guava/RedisBloom)
缓存·guava
xdpcxq10291 个月前
Spring AOP + Guava RateLimiter 用注解实现优雅限流
spring·wpf·guava
ejinxian1 个月前
Google Guava实战
guava·工具库
程序员乐只1 个月前
基于Python+Django+SSM热门旅游景点推荐系统(源码+LW+调试文档+讲解等)/热门旅游地推荐平台/旅游景点推荐软件/热门景点推荐系统/旅游推荐系统/旅游景点热门推荐
spring boot·spring·tomcat·hibernate·java-zookeeper·guava·java-consul
沛沛老爹2 个月前
2025年java总结:缝缝补补又一年?
java·开发语言·人工智能·python·guava·总结·web转型ai
PacosonSWJTU2 个月前
Guava缓存使用入门
java·缓存·guava