/jedis-pojo

Simple self-loading pojo cache service based on Jedis

Primary LanguageJava

jedis-pojo

Simple self-loading pojo cache service based on Jedis. You can find the latest release on Maven Central: http://search.maven.org under:

  • Group ID: io.interact
  • Artifact ID: jedis-pojo
Usage:

Here's an example the shows you how to use the self-loading capability of the cache. You basically pass a Supplier with a lambda to the CacheService, which is invoked when the instance was not found.

// Get an instance of MyModelClass from the cache located at endpoint 'localhost' when it was found.
// Otherwise, get it from the MyModelDao and put it in the cache with a TTL of 5 minutes.
CacheService cache = CacheServiceImpl.getInstance("localhost"));
MyModelClass result = cache.get(key, () -> {
                return MyModelDao.get(key);
            } , MyModelClass.class, 300);

Note that you can use a custom port as well:

CacheService cache = CacheServiceImpl.getInstance("localhost:12345"));

Here's an example of the put and evict methods:

CacheService cache = CacheServiceImpl.getInstance("localhost"));
// Put myInstance in the cache for a minute.
cache.put(key, myInstance, 60);

// And evict it
cache.evict(key);

This library also implements redis set capabilities:

CacheService cache = CacheServiceImpl.getInstance("localhost");
// Add instances to my set. 
Set<MyInstance> instances = new HashSet<>(Arrays.asList(myInstance1, myInstance2));
cache.addToSet(key, instances);

// Fetch set content
Set<MyInstance> cachedInstances = cache.getMembers(key, () -> dao.getSet(key), MyInstance.class);

//evict cache
cache.evict(key);

It is also possible to do batch inserts and deletes to the redis sets:

Map<String, Set<Object>> inserts = new HashMap<>();
inserts.put(key, new HashSet<>(Arrays.asList(myInstance)));
inserts.put(key2, new HashSet<>(Arrays.asList(otherInstance)));

Map<String, Set<Object>> deletes = new HashMap<>();
deletes.put(key3, new HashSet<>(Arrays.asList(myInstance2)));

cache.bulkSetInsertAndDelete(deletes, inserts);
Optional:

Exclude jackson-databind to avoid version conflicts, which can typically happen when it is loaded as a transitive dependency by e.g. Dropwizard and Jersey.

<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>