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>
相关推荐
四季夏目天下第一5 天前
Guava学习(一)
java·开发语言·guava
10km1 个月前
guava:基于TypeToken解析泛型类的类型变量(TypeVariable)的具体类型
java·guava·泛型·typevariable·类型变量
咖猫2 个月前
Guava 库中的 `Multimap` 是一个允许一个键对应多个值的集合 Guava `Multimap` 的基本代码示例:
开发语言·python·guava
咖猫2 个月前
Google guava 最佳实践 学习指南之08 `BiMap`(双向映射)
java·开发语言·guava
小沈同学呀2 个月前
Java 本地缓存实现:Guava Cache、Caffeine、Ehcache 和 Spring Cache
java·缓存·guava·caffeine·ehcache
程序无涯海2 个月前
【Java技巧】深入浅出 Guava Retry 框架:业务兜底重试方案示例
java·开发语言·编程·guava·重试
咖猫2 个月前
Guava 提供了集合操作 `List`、`Set` 和 `Map` 三个工具类
windows·list·guava
咖猫2 个月前
Guava库中的`ImmutableCollections`进行集合操作的示例
java·开发语言·guava
咖猫2 个月前
google 的guava 学习 基本工具类
学习·guava
咖猫2 个月前
Guava 库中的 `Multiset` 是一个允许元素重复的集合
开发语言·python·guava