DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • Google Guava and Its Two Fantastic Libraries: Graph and Eventbus

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Why Role-Based Access Control Fails for AI Agents — and How to Fix It With Memory Lineage Engines
  • Designing Frontend Applications for Failure, Not Just Success

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Caching with Guava

Caching with Guava

Guava cache is a simple library that provides flexible and powerful caching features.

By 
Yusuf Aytaş
·
Sep. 27, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
69.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In computer science, cache is a component that is used to speed up data retrieval in general. The data stored in cache is limited so a given query can hit or miss the data that we are looking for. Caches are generally small in terms of storage because we want it to be fast. There are lots of cache types like CPU Cache, Disk Cache, Web Cache and more. There are several implementations of caches spreading out simple caches to complex caches in Java. Google’s guava library also provides cache mechanism. In this post, we will consider guava cache mechanism and some coding examples for guava.

Guava cache is a simple library that provides flexible and powerful caching features. As guava developers explain, guava cache can be used when fast access needed and when values retrieved multiple times. Generally those needs described above can be catered by  ConcurrentMap implementation; however, a cache removes entries automatically by given constraints like time, size and etc. As a result, automatic eviction of entries makes cache better in terms of load-balancing. To do so guava library implements cache interface described below.

Guava Library cache interface allows standard caching operations like get, put and invalidate. Get operation returns the value associated by the key, put operation stores value associated by the key and invalidate operation discards the value associated with the key. In order to get values that are not currently in cache, there is CacheLoader interface that implements load operation. Moreover, if one want to modify the value that is returned, he can make use of Callable interface that implements call(modifies returned value) operation. Let’s move the coding part.

In our first class, we implement a cache that uses keys to retrieve a person. We define out cache to have a maximum size of 100(it is a very low value, generally higher values are preferred) person. We want to expire keys after 10 seconds of latest access. Moreover, we load our keys by the help of a CacheLoader defined by loader. Lastly, we add a removal listener to catch removal events. In this implementation, we have just used some of the functions that are provided by guava. However, we could use other functions like refreshAfterWrite, expireAfterWrite and so on.

package com.yusufaytas.examples.guava;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;

@Component
public class PersonCache {

LoadingCache cache;

@Autowired
PersonCacheLoader loader;

@Autowired
PersonRemovalListener listener;

public void init(){
cache = CacheBuilder.newBuilder().
maximumSize(100).
expireAfterAccess(10, TimeUnit.SECONDS).
removalListener(listener).
build(loader);
}

public Person get(String key) throws ExecutionException{
return cache.get(key);
}
}

In PersonCacheLoader class, we implement CacheLoader interface. We simply load Person by the help of PersonSerializer which have deserialize method uses key to create Person from file.

package com.yusufaytas.examples.guava;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.cache.CacheLoader;

@Component
public class PersonCacheLoader extends CacheLoader{

@Autowired
PersonSerializer personSerializer;

public Person load(String key) throws Exception {
return personSerializer.deserialize(key);
}
}

We have lastly a removal listener that logs the removed Person’s key.

package com.yusufaytas.examples.guava;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

@Component
public class PersonRemovalListener implements RemovalListener{

Logger logger = LoggerFactory.getLogger(PersonRemovalListener.class);

public void onRemoval(RemovalNotification notification) {
logger.info("Person associated with the key("+
notification.getKey()+ ") is removed.");
}

}

In implementation of those classes we have used spring framework which handles dependencies by the help of annotations(Autowired,Component). The ones who are not familiar with spring can use the classes by creating them manually. Consequently, we tried to talk about general cache, guava cache and gave sample codes. You can download sample code by this link.

 

 

 

 

 

 

Web cache Google Guava

Published at DZone with permission of Yusuf Aytaş. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • Google Guava and Its Two Fantastic Libraries: Graph and Eventbus

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook