- 浏览: 2552649 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
Update Site will come soon
2021-06-02 04:10 1679I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 356Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 405PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 721Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 295Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 295Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 241MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 294MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 325Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 312Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 332Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 283Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 326K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 362Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 444Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ...
相关推荐
Guava Cache是Google Guava库中的一个强大特性,它提供了高效的本地缓存解决方案,用于存储经常访问的数据,以减少对远程服务或计算的调用,从而提高应用性能。本案例代码将详细介绍Guava Cache的使用,包括缓存的...
Guava Cache是Google Guava库中的一个强大特性,它提供了高效的本地缓存解决方案。在许多应用程序中,我们经常需要缓存数据以减少不必要的数据库查询或远程服务调用,提高系统性能。尽管分布式缓存如Redis在高并发和...
GuavaCache是Google开源的一款本地缓存机制,具有简单、强大、轻量级的特点。SpringBoot整合GuavaCache缓存过程解析是指使用SpringBoot框架将GuavaCache整合到应用程序中,以提高应用程序的性能和响应速度。 Guava...
在IT行业中,Google Guava库是一个非常强大的工具集,它为Java开发人员提供了一系列实用的集合、缓存、并发和I/O工具。本篇文章将详细探讨如何利用Guava库实现定时缓存功能,以提高应用的性能和效率。 首先,Guava...
Guava Cache 是一个高性能的缓存框架,由 Google 开发维护。它提供了一个灵活的缓存机制,可以根据实际需求进行配置。 首先,我们需要在 pom.xml 文件中添加 Guava 依赖项,以便使用 Guava Cache。添加以下依赖项:...
Guava Cache是Google Guava库中提供的一种高效、易用的本地缓存解决方案,它可以帮助减少对数据库或远程服务的频繁访问,提高应用程序的性能。Guava Cache支持自动过期、预加载、监听器等特性,使得在Java应用中实现...
Guava Cache是Google Guava库中的一个功能强大的本地缓存实现,它为Java开发者提供了高效、易用的缓存解决方案。Guava Cache的设计目标是为了帮助应用程序在内存中存储经常访问的数据,以减少对远程服务或者数据库的...
├─Google Guava 第01讲-Joiner详细介绍以及和Java8Collector对比.wmv ├─Google Guava 第02讲-Guava Splitter详细讲解以及实战练习...├─Google Guava 第34讲-Guava Cache之RecordStats,CacheBuilderSpec详解.wmv
SpringBoot 使用 GuavaCache 实现简单缓存处理 在本篇文章中,我们将介绍如何使用 GuavaCache 在 SpringBoot 项目中实现简单的缓存处理。缓存机制可以减少对外部服务的查询请求,从而提高应用程序的性能。 问题...
Google Guava是一个由Google开发并维护的开源Java库,它为Java开发者提供了许多实用的工具类和集合框架,极大地简化了常见的编程任务。这个框架包含了多个模块,如基础(base)、缓存(cache)、I/O(io)以及并发...
2. Guava Cache:Google的Guava库中的Cache模块,是一个强大的本地缓存解决方案,支持线程安全、容量限制、自动过期等功能,适用于需要高性能缓存场景。 二、集成Guava Cache 在Spring Boot项目中,我们需要以下...
Guava Cache 是 Google 开源库 Guava 提供的一种高效、易用的缓存解决方案,它旨在帮助开发者在 Java 应用程序中实现本地缓存策略,以提高性能和减少对远程服务的调用。在这个名为 "Guava-Cache-Demo" 的项目中,...
**Google Guava官方教程** Google Guava 是一个广泛使用的 Java 库,它提供了一系列现代编程实用工具,旨在简化常见的编程任务。Guava 提供了集合框架的扩展、并发支持、缓存机制、字符串处理工具、I/O 工具以及...
Guava的Cache模块提供了一种高效的缓存解决方案,它支持自动加载、过期策略、大小限制等功能。开发者可以通过简单API实现数据的缓存,提高系统的响应速度,同时减少不必要的计算或数据库查询。 三、并发工具 Guava...
此外,Guava的Cache模块则提供了一种内存缓存机制,可以根据预设策略自动清除过期的数据,有效提升了应用性能。 Guava对原生类型(如int、char等)提供了泛型支持,避免了装箱拆箱带来的额外开销。例如,Ints、...
**Google Guava官方教程概述** Google Guava 是一个开源库,为Java开发人员提供了一组核心库,包括集合、缓存、并发工具、I/O工具、字符串处理、实用方法等。这个官方教程主要针对Guava库的使用进行详细介绍,帮助...
import com.google.common.cache.*; public class CacheManager { private static final long GUAVA_CACHE_SIZE = 100000; private static final long GUAVA_CACHE_DAY = 10; private static LoadingCache, ...
3. **并发工具**:Guava 提供了线程安全的数据结构,如 LoadingCache 和 Lists.newConcurrentList(),以及原子类和工作队列,帮助开发者编写高效且线程安全的代码。 4. **函数式编程**:Guava 支持函数式编程风格,...
《谷歌Guava库详解》 谷歌Guava,全称为Google Guava,是Google推出的一款Java库,它包含一系列基础工具类,旨在简化Java开发工作,提高代码效率和可维护性。Guava的核心特性包括集合框架、缓存、原始类型支持、...
**谷歌Guava库详解** 谷歌Guava库是一个广泛使用的Java库,它提供了许多核心库的实用工具类,包括集合、缓存、并发、I/O、字符串处理、泛型 utilities、错误处理、网络、数学运算以及类加载器支持等多个方面。Guava...