Coder Social home page Coder Social logo

java-cache's Introduction

java-cache v0.1

A simple cache api supports the following cache services

  • EhCache
  • Memcached
  • Built-in ConcurrentMap based cache
  • Built-in NoCache service

Usage

  • Get a CacheService instance
// Get EhCache service
CacheService cache = CacheServiceProvider.Impl.EhCache.get();
// Get Memcached service
CacheService cache = CacheServiceProvider.Impl.Memcached.get();
// Get Simple In Memory cache service
CacheService cache = CacheServiceProvider.Impl.Simple.get();
// Get No Cache (Null) Service
CacheService cache = CacheServiceProvider.Impl.NoCache.get();
// Automatically detect cache service provider
CacheService cache = CacheServiceProvider.Impl.Auto.get();

// Get a cache service with name specified
CacheService cache = CacheServiceProvider.Impl.XX.get(myCacheName);

Note, Memcached service always returns the same CacheService instance even if a name is specified

When CacheServiceProvider.Impl.Auto is used to get the CacheService, it will first try to load Memcached cache service, if failed, then try the EhCache service; if it still failed, then load the in memory Simple cache service.

  • Use Cache service
// Add an object to cache
cache.put(key, object, ttl); // ttl is the int value specify the object's time to live in seconds
// Add an object to cache with default ttl
cache.put(key, object);
// Get an object from cache by key
Object obj = cache.get(key);
// evict a cache entry by key
cache.evict(key);
  • Start/Stop a cache service
cache.startup();
cache.shutdown();

Note, not these two methods are only used with the Simple cache service, which start up/shutdown a timer thread. Memcached and EhCache service does not use these methods

  • Use Cache facade to quickly access auto discovered cache service function
Cache.startup();
Cache.put(key, object);
object = Cache.get(key);
Cache.evict(key);
Cache.shutdown();

Configuration

  • Simple Cache service

There is nearly not configuration required to use Simple cache service. The only thing you can do is to Set the default ttl:

CacheService cache = CacheServiceProvider.Impl.Simple.get();
cache.setDefaultTTL(120); // set the default ttl to 2 minutes
  • EhCache service

Just follow the EhCache configuration document to configure EhCache service. Mainly what you need is to put a file named ehcache.xml into the class path.

  • Memcached service

The simplest way to configure memcached cache service is to set memcached.host system property to your memcached host. It should contains an ip address and a port number separated by :. If the port part is not provided, then default 11211 port is used. If your memcached service needs username/password to access, you can set those configurations in memcached.username and memcached.password.

Instead of using system properties, you can also set the memcached configuration in memcached.properties file which could be loaded from the class path.

java-cache's People

Contributors

greenlaw110 avatar

Stargazers

Richard Perrott avatar Restmad avatar  avatar robin avatar  avatar

Watchers

 avatar James Cloos avatar Joe Cincotta avatar  avatar

Forkers

xialinlin restmad

java-cache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.