一、前言
基于directmemory-solr源码包中的org.apache.directmemory.solr.SolrOffHeapCache实现类,实现apache的solr定义的org.apache.solr.search.SolrCache缓存接口,具体代码如下。
二、源码说明
package org.apache.directmemory.solr;@b@@b@import java.io.IOException;@b@import java.net.URL;@b@import java.util.Map;@b@import java.util.concurrent.atomic.AtomicLong;@b@import org.apache.directmemory.DirectMemory;@b@import org.apache.directmemory.cache.CacheService;@b@import org.apache.directmemory.measures.Monitor;@b@import org.apache.directmemory.measures.Ram;@b@import org.apache.directmemory.serialization.Serializer;@b@import org.apache.directmemory.serialization.SerializerFactory;@b@import org.apache.directmemory.serialization.SerializerNotFoundException;@b@import org.apache.solr.common.util.NamedList;@b@import org.apache.solr.common.util.SimpleOrderedMap;@b@import org.apache.solr.core.SolrInfoMBean.Category;@b@import org.apache.solr.search.CacheRegenerator;@b@import org.apache.solr.search.SolrCache;@b@import org.apache.solr.search.SolrCache.State;@b@import org.apache.solr.search.SolrIndexSearcher;@b@import org.slf4j.Logger;@b@@b@public class SolrOffHeapCache<K, V>@b@ implements SolrCache<K, V>@b@{@b@ private CumulativeStats stats;@b@ private long lookups;@b@ private long hits;@b@ private long inserts;@b@ private long evictions;@b@ private long warmupTime;@b@ private String name;@b@ private int autowarmCount;@b@ private SolrCache.State state;@b@ private CacheRegenerator regenerator;@b@ private String description;@b@ private CacheService<K, V> cacheService;@b@@b@ public SolrOffHeapCache()@b@ {@b@ this.warmupTime = 0L;@b@@b@ this.description = "DM Cache";@b@ }@b@@b@ public CacheService<K, V> getCacheService()@b@ {@b@ return this.cacheService;@b@ }@b@@b@ public Object init(Map args, Object persistence, CacheRegenerator regenerator)@b@ {@b@ Object buffers = args.get("buffers");@b@ String sizeStr = String.valueOf(args.get("size"));@b@ int limit = (sizeStr == null) ? 1024 : Integer.parseInt(sizeStr);@b@ String initialSizeValue = (String)args.get("initialSize");@b@ int initialSize = Math.min((initialSizeValue == null) ? 1024 : Integer.parseInt(initialSizeValue), limit);@b@@b@ Serializer serializer = null;@b@ String serializerClassName = (String)args.get("serializerClassName");@b@ if (serializerClassName != null)@b@ {@b@ try@b@ {@b@ serializer = SerializerFactory.createNewSerializer(serializerClassName);@b@ }@b@ catch (SerializerNotFoundException e)@b@ {@b@ log.warn("Serializer not found for class " + serializerClassName + ", falling back to the default serializer", e);@b@@b@ serializer = SerializerFactory.createNewSerializer();@b@ }@b@ }@b@@b@ this.cacheService = new DirectMemory().setNumberOfBuffers((buffers != null) ? Integer.valueOf(String.valueOf(buffers)).intValue() : 1).setInitialCapacity(Ram.Mb(Double.valueOf(initialSize).doubleValue() / 512.0D)).setSize(Ram.Mb(limit / 512)).setSerializer(serializer).newCacheService();@b@@b@ this.state = SolrCache.State.CREATED;@b@ this.regenerator = regenerator;@b@ this.name = ((String)args.get("name"));@b@ String autoWarmCount = (String)args.get("autowarmCount");@b@ this.autowarmCount = ((autoWarmCount == null) ? 0 : Integer.parseInt(autoWarmCount));@b@@b@ this.description = "Solr OffHeap Cache(maxSize=" + limit + ", initialSize=" + initialSize;@b@ if (this.autowarmCount > 0)@b@ {@b@ SolrOffHeapCache tmp331_330 = this; tmp331_330.description = tmp331_330.description + ", autowarmCount=" + this.autowarmCount + ", regenerator=" + regenerator;@b@ }@b@ this.description += ')';@b@@b@ if (persistence == null)@b@ {@b@ persistence = new CumulativeStats(null);@b@ }@b@@b@ this.stats = ((CumulativeStats)persistence);@b@@b@ return persistence;@b@ }@b@@b@ public String name()@b@ {@b@ return this.name;@b@ }@b@@b@ public int size()@b@ {@b@ return Long.valueOf(this.cacheService.entries()).intValue();@b@ }@b@@b@ public V put(K key, V value)@b@ {@b@ synchronized (this)@b@ {@b@ if (this.state == SolrCache.State.LIVE)@b@ {@b@ this.stats.inserts.incrementAndGet();@b@ }@b@@b@ this.inserts += 1L;@b@ this.cacheService.put(key, value);@b@ return value;@b@ }@b@ }@b@@b@ public V get(K key)@b@ {@b@ synchronized (this)@b@ {@b@ Object val = this.cacheService.retrieve(key);@b@ if (this.state == SolrCache.State.LIVE)@b@ {@b@ this.lookups += 1L;@b@ this.stats.lookups.incrementAndGet();@b@ if (val != null)@b@ {@b@ this.hits += 1L;@b@ this.stats.hits.incrementAndGet();@b@ }@b@ }@b@ return val;@b@ }@b@ }@b@@b@ public void clear()@b@ {@b@ synchronized (this)@b@ {@b@ this.cacheService.clear();@b@ }@b@ }@b@@b@ public void setState(SolrCache.State state)@b@ {@b@ this.state = state;@b@ }@b@@b@ public SolrCache.State getState()@b@ {@b@ return this.state;@b@ }@b@@b@ public void warm(SolrIndexSearcher searcher, SolrCache<K, V> old)@b@ throws IOException@b@ {@b@ }@b@@b@ public void close()@b@ {@b@ this.cacheService.dump();@b@ Monitor.dump();@b@ }@b@@b@ public String getName()@b@ {@b@ return SolrOffHeapCache.class.getName();@b@ }@b@@b@ public String getVersion()@b@ {@b@ return "1.0";@b@ }@b@@b@ public String getDescription()@b@ {@b@ return this.description;@b@ }@b@@b@ public SolrInfoMBean.Category getCategory()@b@ {@b@ return SolrInfoMBean.Category.CACHE;@b@ }@b@@b@ public String getSourceId()@b@ {@b@ return null;@b@ }@b@@b@ public String getSource()@b@ {@b@ return null;@b@ }@b@@b@ public URL[] getDocs()@b@ {@b@ return new URL[0];@b@ }@b@@b@ public NamedList getStatistics()@b@ {@b@ NamedList lst = new SimpleOrderedMap();@b@ synchronized (this)@b@ {@b@ lst.add("lookups", Long.valueOf(this.lookups));@b@ lst.add("hits", Long.valueOf(this.hits));@b@ lst.add("hitratio", calcHitRatio(this.lookups, this.hits));@b@ lst.add("inserts", Long.valueOf(this.inserts));@b@ lst.add("evictions", Long.valueOf(this.evictions));@b@ lst.add("size", Long.valueOf(this.cacheService.entries()));@b@ }@b@@b@ lst.add("warmupTime", Long.valueOf(this.warmupTime));@b@@b@ long clookups = this.stats.lookups.get();@b@ long chits = this.stats.hits.get();@b@ lst.add("cumulative_lookups", Long.valueOf(clookups));@b@ lst.add("cumulative_hits", Long.valueOf(chits));@b@ lst.add("cumulative_hitratio", calcHitRatio(clookups, chits));@b@ lst.add("cumulative_inserts", Long.valueOf(this.stats.inserts.get()));@b@ lst.add("cumulative_evictions", Long.valueOf(this.stats.evictions.get()));@b@@b@ return lst;@b@ }@b@@b@ private Object calcHitRatio(long clookups, long chits)@b@ {@b@ if (this.lookups == 0L)@b@ {@b@ return "0.00";@b@ }@b@ if (this.lookups == this.hits)@b@ {@b@ return "1.00";@b@ }@b@ int hundredths = (int)(this.hits * 100L / this.lookups);@b@ if (hundredths < 10)@b@ {@b@ return "0.0" + hundredths;@b@ }@b@ return "0." + hundredths;@b@ }@b@@b@ public String toString()@b@ {@b@ return this.name + getStatistics().toString();@b@ }@b@@b@ private static class CumulativeStats@b@ {@b@ AtomicLong lookups;@b@ AtomicLong hits;@b@ AtomicLong inserts;@b@ AtomicLong evictions;@b@@b@ private CumulativeStats()@b@ {@b@ this.lookups = new AtomicLong();@b@@b@ this.hits = new AtomicLong();@b@@b@ this.inserts = new AtomicLong();@b@@b@ this.evictions = new AtomicLong();@b@ }@b@ }@b@}