Local cache
The local cache is stored in memory and the cache is implemented as follows
First you need to introduce a package
<dependency> <groupId>net.sf.ehcache</groupId> <artifactid>ehcache</ Artifactid> <version>2.10.1</version></dependency>
Cache Service Interface:
Package Com.mobcb.platform.service.common;import Net.sf.ehcache.cache;public Interface Ehcacheservice {public void ClearCache (String cachename, String cacheKey); public void Putcache (string cachename, String CacheKey, Object value); Public Object Getcachevalue (string cachename, String cacheKey); Public Cache GetCache (String cachename); /** * Set Cache * * @param cachename cache name * @param cacheKey cache Key * @param value value * @param timetoliveseconds existence time, per second * /public void Putcache (string cachename, String CacheKey, Object Value, int timetoliveseconds);}
Ehcacheservice Interface Implementation:
Package Com.mobcb.platform.service.impl.common;import Com.mobcb.platform.service.common.ehcacheservice;import Javax.annotation.resource;import Net.sf.ehcache.cache;import Net.sf.ehcache.cachemanager;import Net.sf.ehcache.element;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.stereotype.Service, @Service ("Ehcacheservice") public class Ehcacheserviceimpl implements Ehcacheservice {private static Log logger = Logfactory.getlog (Ehcacheserviceimpl.class); @Resource (name = "Ehcachemanager") private CacheManager Cachemanger; Private Boolean createcacheifnotfound = true; @Override public void ClearCache (string cachename, String cacheKey) {Cache cache = GetCache (cachename); if (cache = = null) {return; } cache.remove (CacheKey); } @Override public void Putcache (string cachename, String CacheKey, Object value) {Cache cache = GetCache (ca Chename); if (cache = = null) {return; } cache.put (New Element (CacheKey, value)); } @Override Public Object getcachevalue (string cachename, String cacheKey) {Cache cache = GetCache (cachename ); if (cache = = null) {return null; } element element = Cache.get (CacheKey); if (element = = NULL | | element.isexpired ()) {return null; } return Element.getobjectvalue (); } @Override Public Cache getcache (String cachename) {Cache cache = Cachemanger.getcache (cachename); if (cache = = NULL && createcacheifnotfound) {cache = (cache) cachemanger.addcacheifabsent (cachename); } if (cache = = null) {Logger.error ("Ehcache:cache not config and not auto created, cachename=" + CacheName); } return cache; } @Override public void Putcache (string cachename, String CacheKey, Object value, int timetoliveseconds) { Cache cache = GetCache (cachename); if (cache = = null) {return; } cache.put (New Element (CacheKey, Value, Timetoliveseconds, timetoliveseconds)); }}
Examples of cache usage:
Introducing the Cache service interface
/** * Cache Service */@Resource (name = "Ehcacheservice") private Ehcacheservice Ehcacheservice;
Call:
Object Requestsource = Ehcacheservice.getcachevalue ( "Shcauth", "Shcauthkey"); Logger.info ("------------------- -----Cache to obtain the authentication code "+requestsource+"-----------------------");
The first parameter is the cache name, and the second is the key value under the cache name.
Redis Cache
Java local cache and Redis cache