Google Guava Cache

本文通过两个示例展示了如何使用 Guava Cache,并解释了其移除缓存数据的方法,包括基于大小和时间的配置。同时,文章还提供了解决在加载缓存时遇到的错误的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google Guava Cache

First of all, add the dependencies as follow:
"com.google.guava" % "guava" % "14.0.1"
"com.google.code.findbugs" % "jsr305" % "2.0.1"

>sbt clean update gen-idea

1. First Example based on CacheLoader
package com.sillycat.easycassandraserver.apps

import com.sillyat.easycassandraserver.models.Product
import com.google.common.cache.{CacheLoader, CacheBuilder, LoadingCache}
import java.util.concurrent.TimeUnit
import org.joda.time.DateTime

object GuavaCacheApp extends App{

val cache: LoadingCache[String, Product] = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(5, TimeUnit.SECONDS)
.build(
new CacheLoader[String, Product](){
def load(key: String): Product = {
val p: Product = Product(None, "Nice Product", DateTime.now)
p
}
})

val book = cache.get("key1")
Console.println("1 hit=" + book)

Thread.currentThread()
Thread.sleep(TimeUnit.SECONDS.toMillis(3))
val book1 = cache.get("key1")
Console.println("2 hit=" + book1)

Thread.currentThread()
Thread.sleep(TimeUnit.SECONDS.toMillis(6))
val book2 = cache.get("key1")
Console.println("3 hit=" + book2)

}

package com.sillyat.easycassandraserver.models
import org.joda.time.DateTime
case class Product(id: Option[Long], productName: String, create: DateTime)


The output will be quite easy>
1 hit=Product(None,Nice Product,2013-06-19T16:43:05.324-05:00)
2 hit=Product(None,Nice Product,2013-06-19T16:43:05.324-05:00)
3 hit=Product(None,Nice Product,2013-06-19T16:43:14.514-05:00)

2. Second Example with Callable Cache
import com.sillyat.easycassandraserver.models.Product
import com.google.common.cache.{Cache, CacheLoader, CacheBuilder, LoadingCache}
import java.util.concurrent.{Callable, TimeUnit}
import org.joda.time.DateTime

..snip…

//callable
val cacheCallable: Cache[String, Product] = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(5, TimeUnit.SECONDS).build()

val product1 = cacheCallable.get("key2",new Callable[Product](){
def call: Product = {
Product(None, "Good Product", DateTime.now)
}
})
Console.println("Product 1 hit=" + product1)

Thread.currentThread()
Thread.sleep(TimeUnit.SECONDS.toMillis(3))
val product2 = cacheCallable.get("key2",new Callable[Product](){
def call: Product = {
Product(None, "Good Product", DateTime.now)
}
})
Console.println("Product 2 hit=" + product2)

Thread.currentThread()
Thread.sleep(TimeUnit.SECONDS.toMillis(6))
val product3 = cacheCallable.get("key2",new Callable[Product](){
def call: Product ={
Product(None, "Good Product", DateTime.now)
}
})
Console.println("Product 3 hit=" + product3)
..snip…

Product 1 hit=Product(None,Good Product,2013-06-19T16:55:56.491-05:00)
Product 2 hit=Product(None,Good Product,2013-06-19T16:55:56.491-05:00)
Product 3 hit=Product(None,Good Product,2013-06-19T16:56:05.494-05:00)

3. Concepts
The ways to remove the cache data:
Based Size and Time
CacheBuilder.maximumSize(long)
expireAfterAccess(long, TimeUnit)
expireAfterWrite(long, TimeUnit)

Manually Call
Cache.invalidate(key)
Cache.invalidate(keys)
Cache.invalidateAll()

And also some other things mentioned in other's blog http://xpchenfrank.iteye.com/blog/1508733

I did not try them, I only write to small scala example to get an idea of what it does in guava.

Tips
1. Error Message:
Error: scala: error while loading Cache, class file
'.ivy2/cache/com.google.guava/guava/jars/guava-14.0.jar(com/google/common/cache/Cache.class)' is broken
(class java.lang.RuntimeException/bad constant pool index: 1025 at pos: 1213)

Solution:
It seems that I need to add one more dependency.
"com.google.code.findbugs" % "jsr305" % "2.0.1"

2. Error Message
not found: value println

Solution:
I change the println to Console.println(), it works. But I do not think this this is the right way.


References:
http://xpchenfrank.iteye.com/category/220687
http://panyongzheng.iteye.com/blog/1720259

https://code.google.com/p/guava-libraries/
https://code.google.com/p/guava-libraries/wiki/GuavaExplained
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值