Java local cache and Redis cache

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.