Guava Cache Redis Save

implement guava cache interface backed by redis

Project README

guava-cache-redis

Implement guava cache interface backed by redis. Guava provide memory cache implementation. We provide redis cache implementation.

Please read guava caches explained first.

Howto

<dependency>
    <groupId>com.github.levyfan</groupId>
    <artifactId>guava-cache-redis</artifactId>
    <version>0.0.2</version>
</dependency>

From a CacheLoader

RedisCache<Key, Graph> redisCache = new RedisCache<>(
  jedisPool,
  keySerializer,
  valueSerializer,
  keyPrefix,
  expiration,
  new CacheLoader<Key, Graph>() {
    public Graph load(Key key) throws AnyException {
      return createExpensiveGraph(key);
    }
  }
);
...

try {
  return redisCache.get(key);
} catch (ExecutionException e) {
  throw new OtherException(e.getCause());
}

From a Callable

RedisCache<Key, Graph> redisCache = new RedisCache<>(
  jedisPool,
  keySerializer,
  valueSerializer,
  keyPrefix,
  expiration
);
...

try {
  // If the key wasn't in the "easy to compute" group, we need to do things the hard way.
  redisCache.get(key, new Callable<Value>() {
    @Override
    public Value call() throws AnyException {
      return doThingsTheHardWay(key);
    }
  });
} catch (ExecutionException e) {
  throw new OtherException(e.getCause());
}
Open Source Agenda is not affiliated with "Guava Cache Redis" Project. README Source: levyfan/guava-cache-redis
Stars
37
Open Issues
0
Last Commit
1 year ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating