1 16 package cintoo.messages.context; 17 18 import api.cintoo.messages.bundle.BaseBundle; 19 import api.cintoo.messages.context.Context; 20 import api.cintoo.messages.context.ContextCache; 21 22 import java.util.HashMap ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.concurrent.locks.ReentrantReadWriteLock ; 26 27 35 public class DefaultContextCache implements ContextCache { 36 private Map <Long , BaseBundle> contextCache; 37 private ReentrantReadWriteLock lock = new ReentrantReadWriteLock (); 38 39 42 public DefaultContextCache() { 43 contextCache = new HashMap <Long , BaseBundle>(); 44 } 45 46 53 public void addToCache(Context context, Locale locale, BaseBundle bundle) { 54 try { 55 lock.writeLock().lock(); 56 contextCache.put(ContextKey.generate(context, locale), bundle); 57 } finally { 58 lock.writeLock().unlock(); 59 } 60 } 61 62 69 public BaseBundle getFromCache(Context context, Locale locale) { 70 BaseBundle bundle = null; 71 try { 72 lock.readLock().lock(); 73 bundle = contextCache.get(ContextKey.generate(context, locale)); 74 } finally { 75 lock.readLock().unlock(); 76 } 77 return bundle; 78 } 79 80 87 public boolean isCached(Context context, Locale locale) { 88 boolean contains = false; 89 try { 90 lock.readLock().lock(); 91 contains = contextCache.containsKey(ContextKey.generate(context, locale)); 92 } finally { 93 lock.readLock().unlock(); 94 } 95 return contains; 96 } 97 98 101 public void clear() { 102 try { 103 lock.writeLock().lock(); 104 try { 105 lock.writeLock().lock(); 106 contextCache.clear(); 107 } finally { 108 lock.writeLock().unlock(); 109 } 110 } finally { 111 lock.writeLock().unlock(); 112 } 113 } 114 } 115 | Popular Tags |