Ehcache - hello world
2015-07-04 15:12
375 查看
Key Classes
CacheManager
The CacheManager class is used to manage caches. Creation of, access to, and removal of caches is controlled by a named CacheManager.Cache
A Cache is a thread-safe logical representation of a set of data elements, analogous to a cache region in many caching systems. Once a reference to a cache is obtained (through a CacheManager), logical actions can be performed. The physical implementation of these actions is relegated to the stores.Element
An element is an atomic entry in a cache. It has a key, a value, and a record of accesses. Elements are put into and removed from caches. They can also expire and be removed by the cache, depending on the cache settings.Creating a CacheManager
Create a new CacheManager or return the existing one named in the configuration.CacheManager.newInstance(Configuration configuration)
Create a new singleton CacheManager with default configuration, or return the existing singleton.
CacheManager.create()
CacheManager.getInstance()
Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.
CacheManager.create(Configuration configuration)
Create a new CacheManager, or throw an exception if the CacheManager named in the configuration already exists or if the parameter (configuration) is null.
new CacheManager(Configuration configuration)
Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.
CacheManager.create(Configuration configuration)
Loading a Configuration
The following creates a CacheManager based on the configuration defined in the ehcache.xml file in the classpath.CacheManager manager = CacheManager.newInstance();
The following creates a CacheManager based on a specified configuration file.
CacheManager manager = CacheManager.newInstance("src/config/ehcache.xml");
The following creates a CacheManager from a configuration resource in the classpath.
URL url = getClass().getResource("/anotherconfigurationname.xml"); CacheManager manager = CacheManager.newInstance(url);
The following creates a CacheManager from a configuration in an InputStream.
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); try { CacheManager manager = CacheManager.newInstance(fis); } finally { fis.close(); }
Performing Basic Cache Operations
Obtaining a reference to a CacheCache cache = manager.getCache("sampleCache1");
Putting an Element in Cache
Cache cache = manager.getCache("sampleCache1"); Element element = new Element("key1", "value1"); cache.put(element);
Updating and Element in Cache
Cache cache = manager.getCache("sampleCache1"); cache.put(new Element("key1", "value1")); //This updates the entry for "key1" cache.put(new Element("key1", "value2"));
Getting an Element from Cache - The following gets a Serializable value from an element with a key of key1 .
Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Serializable value = element.getValue();
Getting an Element from Cache - The following gets a NonSerializable value from an element with a key of key1 .
Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Object value = element.getObjectValue();
Removing an Element from Cache
Cache cache = manager.getCache("sampleCache1"); cache.remove("key1");
Obtaining Cache Sizes - The following gets the number of elements currently in the cache.
Cache cache = manager.getCache("sampleCache1"); int elementsInMemory = cache.getSize();
Obtaining Cache Sizes - The following gets the number of elements currently in the MemoryStore.
Cache cache = manager.getCache("sampleCache1"); long elementsInMemory = cache.getMemoryStoreSize();
Obtaining Cache Sizes - The following gets the number of elements currently in the DiskStore.
Cache cache = manager.getCache("sampleCache1"); long elementsInMemory = cache.getDiskStoreSize();
Shutdown the CacheManager
The following shuts down the singleton CacheManager:CacheManager.getInstance().shutdown();
The following shuts down a CacheManager instance, assuming you have a reference to the CacheManager called cacheManager :
cacheManager.shutdown();
相关文章推荐
- 白盒测试 语句覆盖、判定覆盖、条件覆盖、判定条件覆盖、条件组合覆盖、路径覆盖
- JSP 和 JavaBean连接sql server验证登录
- poj1129(没有优化)
- App开发到App Store上架,发布流程。
- 《C语言及程序设计》实践参考——学生成绩统计
- Android开发中遇到的问题(五)——Eclipse导入Android项目出现"Invalid project description overlaps the location of another project"错误的解决办法
- tolua 有些功能可以用(经过测试)
- 记录几款不错的chrome主题
- 记住密码
- C++atoi与atof
- Android开发中遇到的问题(四)——Android中WARNING: Application does not specify an API level requirement!的解决方法
- 五、Spring源码分析——Spring Aop
- 用jsp写一个动态表格
- iOS中避免类的循环引用和协议一起使用
- 【计算机网络学习】之TCP和UDP比较
- tyvj-1460 旅行
- Java面试宝典2015版
- Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件
- SQLite数据库连接字符串(C#)
- boxplot--盒图