Ibatis之缓存插件

 

缓存类型 Types in [MEMORY、LRU、FIFO、OSCACHE]

Ibatis中实现com.ibatis.sqlmap.engine.cache. CacheController  [inteface]

public void flush(CacheModel cacheModel);/*Flush*/

public Object getObject(CacheModel cacheModel, Object key);/*Get*/

public Object removeObject(CacheModel cacheModel, Object key);/*Remove*/

public void putObject(CacheModel cacheModel, Object key, Object object);/*Add */

public void configure(Properties props);/*Set*/

 

如果Ibatis自带的缓存策略不适合你的具体应用,可以实现CacheController  Interface来实现自己的方法

 

一:MEMORY  内存缓存,和Java内存管理机制类似分为[SOFT、WEAK、STRONG]

 <property name="reference-type"  value="SOFT|WEAK|STRONG"/>

SOFT:

只有当目前内存不足的情况下,JVM 在垃圾回收时才会收回其包含的引用(JVM 并不是只有当内存不足时才启动垃圾回收机制,何时进行垃圾回收取决于各版本JVM 的垃圾回收策略。如某这垃圾回收策略为:当系统目前较为空闲,且无效对象达到一定比 率时启动垃圾回收机制,此时的空余内存倒可能还比较充裕)

 

WEAK:

WeakReference 比SoftReference 在引用的维持性上来看更加微弱。无需等到内存不足的情况,只要JVM 启动了垃圾回收机制,那么WeakReference 所对应的对象就将被JVM 回收。相对SoftReference 而言,WeakReference 被JVM 回收的概率更大。

 

STRONG:

所引用的对象几乎无法被回收重用。它指向的对象实际上已经被JVM 销毁(finalize 方法已经被执行),只是暂时还没被垃圾回收器收回而已。PhantomReference 主要用于辅助对象的销毁过程,在实际应用层研发中,几乎不会涉及。

 

其控制器com.ibatis.sqlmap.engine.cache.memory.MemoryCacheController

private MemoryCacheLevel cacheLevel = MemoryCacheLevel.WEAK;

private Map cache = Collections.synchronizedMap(new HashMap());/*Map 存储结构*/

 

二:LRU 最近最少使用算法

<property name="size" value="Number"/>

其控制器com.ibatis.sqlmap.engine.cache.lru.LruCacheController

private Map cache<Collections.synchronizedMap(new HashMap())>;
private List keyList<Collections.synchronizedList(new LinkedList())>;

Put算法

  public void putObject(CacheModel cacheModel, Object key, Object value) {
    cache.put(key, value);
    keyList.add(key);
    if (keyList.size() > cacheSize) {
      try {
        Object oldestKey = keyList.remove(0);
        cache.remove(oldestKey);
      } catch (IndexOutOfBoundsException e) {
        //ignore
      }
    }
  }

Get算法

  public Object getObject(CacheModel cacheModel, Object key) {
    Object result = cache.get(key);
    keyList.remove(key);
    if (result != null) {
      keyList.add(key);
    }
    return result;
  }

 

使用[沉淀]技巧把最近最少使用的沉淀到最前面

 

 

FIFO:

<property name="size" value="Number"/>

其控制器com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController

  public void putObject(CacheModel cacheModel, Object key, Object value) {
    cache.put(key, value);
    keyList.add(key);
    if (keyList.size() > cacheSize) {
      try {
        Object oldestKey = keyList.remove(0);
        cache.remove(oldestKey);
      } catch (IndexOutOfBoundsException e) {
        //ignore
      }
    }
  }

public Object getObject(CacheModel cacheModel, Object key) {
    return cache.get(key);
  }

 

 

OSCACHE:使用oscache自带的配置文件oscache.properties

# CACHE IN MEMORY
#
# If you want to disable memory caching, just uncomment this line.
#
 cache.memory=false  /*是否缓存到内存中,一般为true,这里为测试缓存到磁盘*/


# CACHE KEY
#
# This is the key that will be used to store the cache in the application
# and session scope.
#
# If you want to set the cache key to anything other than the default
# uncomment this line and change the cache.key
#
 cache.key=__oscache_cache


# USE HOST DOMAIN NAME IN KEY
#
# Servers for multiple host domains may wish to add host name info to
# the generation of the key.  If this is true, then uncomment the
# following line.
#
# cache.use.host.domain.in.key=true


# CACHE LISTENERS
#
# These hook OSCache events and perform various actions such as logging
# cache hits and misses, or broadcasting to other cache instances across a cluster.
# See the documentation for further information.
#
# cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener,
#                       com.opensymphony.oscache.extra.CacheEntryEventListenerImpl,
#                       com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl,
#                       com.opensymphony.oscache.extra.ScopeEventListenerImpl


# CACHE PERSISTENCE CLASS
#
# Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,
# don't forget to supply the cache.path property to specify the location of the cache
# directory.
#
# If a persistence class is not specified, OSCache will use memory caching only.
#
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener

# CACHE OVERFLOW PERSISTENCE
# Use persistent cache in overflow or not. The default value is false, which means
# the persistent cache will be used at all times for every entry.  true is the recommended setting.
#
# cache.persistence.overflow.only=true

# CACHE DIRECTORY
#
# This is the directory on disk where caches will be stored by the DiskPersistenceListener.
# it will be created if it doesn't already exist. Remember that OSCache must have
# write permission to this directory.
#
# Note: for Windows machines, this needs \ to be escaped
# ie Windows:
cache.path=c:\\myapp\\cache
# or *ix:
# cache.path=/opt/myapp/cache
#
# cache.path=c:\\app\\cache


# CACHE ALGORITHM
#
# Default cache algorithm to use. Note that in order to use an algorithm
# the cache size must also be specified. If the cache size is not specified,
# the cache algorithm will be Unlimited cache.
#


cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache/*缓存淘汰算法*/


# cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache

# THREAD BLOCKING BEHAVIOR
#
# When a request is made for a stale cache entry, it is possible that another thread is already
# in the process of rebuilding that entry. This setting specifies how OSCache handles the
# subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve
# the old content to subsequent threads until the cache entry has been updated. This provides
# the best performance (at the cost of serving slightly stale data). When blocking is enabled,
# threads will instead block until the new cache entry is ready to be served. Once the new entry
# is put in the cache the blocked threads will be restarted and given the new entry.
# Note that even if blocking is disabled, when there is no stale data available to be served
# threads will block until the data is added to the cache by the thread that is responsible
# for building the data.
#
# cache.blocking=false

# CACHE SIZE
#
# Default cache size in number of items. If a size is specified but not
# an algorithm, the cache algorithm used will be LRUCache.
#
cache.capacity=1000 /*Size*/


# CACHE UNLIMITED DISK
# Use unlimited disk cache or not. The default value is false, which means
# the disk cache will be limited in size to the value specified by cache.capacity.
#
# cache.unlimited.disk=false


# JMS CLUSTER PROPERTIES
#
# Configuration properties for JMS clustering. See the clustering documentation
# for more information on these settings.
#
#cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory
#cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic
#cache.cluster.jms.node.name=node1


# JAVAGROUPS CLUSTER PROPERTIES
#
# Configuration properites for the JavaGroups clustering. Only one of these
# should be specified. Default values (as shown below) will be used if niether
# property is set. See the clustering documentation and the JavaGroups project
# (www.javagroups.com) for more information on these settings.
#
#cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\
#mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\
#PING(timeout=2000;num_initial_members=3):\
#MERGE2(min_interval=5000;max_interval=10000):\
#FD_SOCK:VERIFY_SUSPECT(timeout=1500):\
#pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\
#UNICAST(timeout=300,600,1200,2400):\
#pbcast.STABLE(desired_avg_gossip=20000):\
#FRAG(frag_size=8096;down_thread=false;up_thread=false):\
#pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)
#cache.cluster.multicast.ip=231.12.21.132

 

 

一般推荐使用OSCACHE这个[专业]缓存插件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值