Ehcache 2.4 User Guide
Total Page:16
File Type:pdf, Size:1020Kb
...................................................................................................................................... Ehcache v. 2.4 User Guide ...................................................................................................................................... Terracotta, Inc. 2011-05-05 T a b l e o f C o n t e n t s i Table of Contents ....................................................................................................................................... 1 Table of Contents . i 2 Preface . 1 3 Introduction . 2 4 Getting Started . 9 5 Dependencies . 11 6 Cache Concepts . 12 7 Configuration . 17 8 Storage Options . 24 9 Cache Consistency Options . 31 10 Cache Eviction Algorithms . 39 11 Big Memory:Off-Heap Store . 42 12 JDBC Caching . 52 13 Spring Caching with Ehcache . 56 14 Code Samples . 58 15 Class loading and Class Loaders . 65 16 Tuning Garbage Collection . 67 17 Cache Decorators . 68 18 Hibernate Caching . 71 19 Web Caching . 83 20 Using ColdFusion with Ehcache . 88 21 Cache Topologies . 91 22 Distributed Caching . 23 Replicated Caching With RMI . 94 24 Replicated Caching With JGroups . 100 25 Replicated Caching With JMS . 104 26 Shutting Down Ehcache . 115 26 Logging . 117 26 Remote Network replication debugging: RMI Replicated Caches . 118 26 JMX Management And Monitoring . 120 26 JTA And Transactions . 127 26 Search . 135 26 Ehcache Monitor . 145 26 Bulk Loading . 26 CacheManager Event Listeners . 151 26 Cache Event Listeners . 154 26 Cache Exception Handlers . 158 26 Cache Extensions . 161 26 Cache Loaders . 164 © 2 0 1 1 , T e r r a c o t t a , I n c . • A L L R I G H T S R E S E R V E D . T a b l e o f C o n t e n t s ii 26 Write-through and write-behind caching with CacheWriters . 168 26 Cache Server with SOAP and RESTful Web Services . 177 26 Explicit Locking API . 195 26 BlockingCache and SelfPopulatingCache . 198 26 OpenJPA Caching . 199 26 Grails Caching . 200 26 JRuby Caching . 202 26 Glassfish HowTo . 206 26 Google App Engine Caching . 208 26 Tomcat Issues and Best Practices . 211 26 JSR107 (JCACHE) Support . 212 26 Building From Source . 218 26 FAQ . 220 © 2 0 1 1 , T e r r a c o t t a , I n c . • A L L R I G H T S R E S E R V E D . 1 P r e f a c e 1 1 Preface ....................................................................................................................................... 1.1 Preface This is a book about Ehcache, a widely used open source Java cache. Ehcache has grown in size and scope since it was introduced in October 2003. As people used it they often noticed it was missing a feature they wanted. Over time, the features that were repeatedly asked for, and make sense for a Cache, have been added. Ehcache is now used for Hibernate caching, data access object caching, security credential caching, web caching, SOAP and RESTful server caching, application persistence and distributed caching. In August 2009, Ehcache was acquired by Terracotta, Inc. and has been continously enhanced since then. 1.1.1 Version This book is for Ehcache version 2.4.1. 1.1.2 Audience The intended audience for this book is developers who use ehcache. It should be able to be used to start from scratch, get up and running quickly, and also be useful for the more complex options. Ehcache is about performance and load reduction of underlying resources. Another natural audience is performance specialists. It is also intended for application and enterprise architects. Some of the features of ehcache, such as distributed caching and Java EE caching, are alternatives to be considered along with other ways of solving those problems. This book discusses the trade-offs in Ehcache's approach to help make a decision about appropriateness of use. 1.1.3 Acknowledgements Ehcache has had many contributions in the form of forum discussions, feature requests, bug reports, patches and code commits. Rather than try and list the many hundreds of people who have contributed to Ehcache in some way it is better to link to the web site where contributions are acknowledged in the following ways: • Bug reports and features requests appear in the changes report here: • Patch contributors generally end up with an author tag in the source they contributed to. • Team members appear on the team list page here: © 2 0 1 1 , T e r r a c o t t a , I n c . • A L L R I G H T S R E S E R V E D . 2 I n t r o d u c t i o n 2 2 Introduction ....................................................................................................................................... 2.1 Introduction Ehcache is a cache library. Before getting into ehcache, it is worth stepping back and thinking about caching generally. 2.1.1 About Caches Wiktionary defines a cache as A store of things that will be required in future, and can be retrieved rapidly. That is the nub of it. In computer science terms, a cache is a collection of temporary data which either duplicates data located elsewhere or is the result of a computation. Once in the cache, the data can be repeatedly accessed inexpensively. 2.1.2 Why caching works 2.1.2.1 Locality of Reference While Ehcache concerns itself with Java objects, caching is used throughout computing, from CPU caches to the DNS system. Why? Because many computer systems exhibit locality of reference. Data that is near other data or has just been used is more likely to be used again. 2.1.2.2 The Long Tail Chris Anderson, of Wired Magazine, coined the term The Long Tail to refer to Ecommerce systems. The idea that a small number of items may make up the bulk of sales, a small number of blogs might get the most hits and so on. While there is a small list of popular items, there is a long tail of less popular ones. The Long Tail The Long Tail is itself a vernacular term for a Power Law probability distribution. They don't just appear in ecommerce, but throughout nature. One form of a Power Law distribution is.