/** *//** * Puts an object in a cache * * @param key The unique key for this cached object * @param content The object to store * @param groups The groups that this object belongs to * @param policy The refresh policy to use */ publicvoid putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy) { // 直接调用的Cache类的#putInCache getCache().putInCache(key, content, groups, policy, null); } // Cache的#putInCache()方法 publicvoid putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy, String origin) { // 首先查找这个key在缓存中是否已经存在,没有就创建一个 CacheEntry cacheEntry =this.getCacheEntry(key, policy, origin); // 判断是否是新创建的缓存 boolean isNewEntry = cacheEntry.isNew(); // [CACHE-118] If we have an existing entry, create a new CacheEntry so // we can still access the old one later // 这里如果不是新的缓存也会新建一个CacheEntry,因为老的缓存值在后边也能访问到 if (!isNewEntry) { cacheEntry =new CacheEntry(key, policy); } cacheEntry.setContent(content); cacheEntry.setGroups(groups); // 放入缓存 cacheMap.put(key, cacheEntry); // Signal to any threads waiting on this update that it's now ready for them in the cache! // 这里会通知其他在等待值的线程结束wait completeUpdate(key); // 针对缓存事件的listener发送事件 if (listenerList.getListenerCount() >0) { CacheEntryEvent event =new CacheEntryEvent(this, cacheEntry, origin); if (isNewEntry) { dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_ADDED, event); }else{ dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_UPDATED, event); } } }
先简单的看看CacheEntry的构造函数吧,这个最基本:
public CacheEntry(String key, EntryRefreshPolicy policy, String[] groups) { // CacheEntry中保存了key,所属的分组(用一个HashSet保存分组的名字,有点像Tag) // 还有刷新策略和创建的时间 this.key = key; if (groups !=null) { this.groups =new HashSet(groups.length); for (int i =0; i < groups.length; i++) { this.groups.add(groups[i]); } } this.policy = policy; this.created = System.currentTimeMillis(); }