Google Guava Cache

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

猜你喜欢

转载自sillycat.iteye.com/blog/1889542