日期:2014-05-16  浏览次数:20439 次

iBATIS中使用EHCache

需要实现CacheController接口并通过一系列的配置委托iBATIS使用EHCache进行缓存.

  1. package com.ibatis.sqlmap.engine.cache.EhCacheController;
  2. import java.net.URL;
  3. import java.util.Properties;
  4. import net.sf.ehcache.Cache;
  5. import net.sf.ehcache.CacheManager;
  6. import net.sf.ehcache.Element;
  7. import com.ibatis.sqlmap.engine.cache.CacheController;
  8. import com.ibatis.sqlmap.engine.cache.CacheModel;
  9. /**
  10. * EhCache Implementation of the {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able to use
  11. * EhCache as a cache implementation in iBatis. You can configure your cache model as follows, by example, in your
  12. * sqlMapping files:
  13. * <cacheModel id="myCache" type="nl.rabobank.springproject.ibatis.EhCacheController" readOnly="true" serialize="false">
  14. *??? <property name="configFile" value="/path-to-ehcache.xml"/>
  15. * </cacheModel>
  16. * Alternatively, you can use a type alias in your type attribute and defining the class with a
  17. * <TypeAlias> declaration, see iBatis documentation on how to do this.
  18. */
  19. public class EhCacheController implements CacheController {
  20. ????/** The EhCache CacheManager. */
  21. ????private CacheManager cacheManager;
  22. ????/**
  23. ????? * Flush a cache model.
  24. ????? * @param cacheModel - the model to flush.
  25. ????? */
  26. ????public void flush(CacheModel cacheModel) {
  27. ???????? getCache(cacheModel).removeAll();
  28. ???? }
  29. ????/**
  30. ????? * Get an object from a cache model.
  31. ????? * @param cacheModel - the model.
  32. ????? * @param key???????? - the key to the object.
  33. ????? * @return the object if in the cache, or null(?).
  34. ????? */
  35. ????public Object getObject(CacheModel cacheModel, Object key) {
  36. ???????? Object result = null;
  37. ???????? Element element = getCache(cacheModel).get(key);
  38. ????????if (element != null) {
  39. ???????????? result = element.getObjectValue();
  40. ???????? }
  41. ????????