Using ThreadLocal to cache data in Request scope

Source: Internet
Author: User
Tags int size

/** * aim to cache the data that‘s accessed frequently and costly. * @param <K> * @param <V> */public interface Cache<K,V> { V get(K k); void put(K k, V v); void clean(); int size();}
/** *this class is a implementor from Cache.and It caches data in thread local. * @see Cache * @see ThreadLocal */public class threadlocalcache<k,v> implements cache<k,v>{private Threadl    ocal<map<k,v>> threadLocal = new threadlocal<> ();    Public threadlocal<map<k, V>> getthreadlocal () {return ThreadLocal;    } public void Setthreadlocal (threadlocal<map<k, v>> ThreadLocal) {this.threadlocal = ThreadLocal;        } private Map<k, v> Getthreadlocalmap () {map<k, v> Map = getthreadlocal (). get ();            if (map = = null) {map = new HashMap ();        Getthreadlocal (). Set (map);    } return map;        } protected void Cleanthreadlocal () {map<k, v> Map = getthreadlocal (). get ();        if (map! = null) map.clear ();    Getthreadlocal (). remove ();        } private int sizethreadlocal () {map<k, v> Map = Threadlocal.get (); return map = = Null?    0:map.size ();    } @Override public V get (k k) {return Getthreadlocalmap (). get (k);    } @Override public void put (k K, v V) {getthreadlocalmap (). put (K,V);    } @Override public void Clean () {cleanthreadlocal ();    } @Override public int size () {return sizethreadlocal (); }}
 /** * This class was to create a threadLocal through Factory. * @see threadlocalfactory * @see com.pretang.cl Oud.service.agent.AgentRpcService */public class Factorythreadlocalcache<k,v> extends Threadlocalcache<k,v        > {public Factorythreadlocalcache () {super ();    Super.setthreadlocal (Threadlocalfactory.getthreadlocal ()); }}
 /** * This class is a building ThreadLocal factory. And to manage the lifetime of the threadLocal.    * @see Factorythreadlocalcache */public abstract class Threadlocalfactory {//a sharing ThreadLocal in heap.    private static final ThreadLocal ThreadLocal = new ThreadLocal ();     /** * Get a threadLocal instance.    * @return */public static ThreadLocal getthreadlocal () {return ThreadLocal; }/** * Clean the threadLocal */public static void Cleanthreadlocal () {Object obj = Threadlocal.get (        );        if (null! = obj && obj instanceof Collection) ((Collection) obj). Clear ();    Threadlocal.remove (); }}
@Component@WebFilterpublic class CustomerFilter implements Filter {    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {        filterChain.doFilter(servletRequest,servletResponse);        // clean the variables in threads for a request        ThreadLocalFactory.cleanThreadLocal();    }    @Override    public void destroy() {    }}
    private static final Cache<Integer, AgentDto> agentDtoCache = new FactoryThreadLocalCache<>();    private static final Cache<Integer, AgentUser> agentUserCache = new FactoryThreadLocalCache<>();

Using ThreadLocal to cache data in Request scope

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.