PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ehcache/README.md

https://github.com/bmabey/clj-cache
Markdown | 121 lines | 73 code | 48 blank | 0 comment | 0 complexity | dc42ae3045cf66c393144eff9152684a MD5 | raw file
  1. # Ehcache support for clj-cache
  2. This plugin allows clj-cache to be used with the java [Ehcache](http://ehcache.org/) library to provide distributed caching and persistence.
  3. A PDF user guide for Ehcache is available [here](http://ehcache.org/documentation/EhcacheUserGuide-1.7.1.pdf).
  4. Ehcache is flexible and powerful but comes with a large API and may require XML configuration. The `clj-cache.ehcache` namespace does its best to isolate casual users from the complexity without limiting access to the underlying java objects and features. However, feedback and feature requests are very welcome.
  5. ## Example using a default Ehcache configuration
  6. (ns an-example
  7. (:use clj-cache.cache)
  8. (:require [clj-cache.ehcache :as ehcache]))
  9. (defn-cached get-user-from-db
  10. (ehcache/strategy)
  11. [username]
  12. ;; Slow database read goes here
  13. )
  14. ## Example using persistence
  15. (defn-cached get-user-from-db
  16. (ehcache/strategy {:max-elements-in-memory 1000
  17. :eternal true
  18. :overflow-to-disk true
  19. :disk-persistent true
  20. :clear-on-flush true})
  21. [username]
  22. ;; Slow database read goes here
  23. )
  24. ## Dependencies
  25. To use Ehcache and clj-cache pull in the following dependency using Leiningen, Cake or Maven:
  26. [clj-cache-ehcache "0.0.4"]
  27. Ehcache uses slf4j to do logging and a slf4j plugin must be included as a dependency. To log to stderr you can use:
  28. [org.sljf4j/slf4j-simple "1.6.1"]
  29. Or if logging is not required:
  30. [org.sljf4j/slf4j-nop "1.6.1"]
  31. ## Limitations
  32. This package assumes all keys and values in the cache are `java.io.Serializable`. This covers most clojure datastructures but means that different versions of clojure (e.g 1.1 and 1.2) shouldn't share the same distributed cache.
  33. Internally, clj-cache uses features found in clojure to limit the number of calls to slow functions on a cache miss. Ehcache can also do this using locking. Locking in Ehcache is relatively new, requires an additional package and is not well documented. Because of this clj-cache does not yet support locking with Ehcache. However, if there is interest, locking can be added at a later date.
  34. # API
  35. ## strategy [] [config] [manager config]
  36. Returns a strategy for use with clj-cache.cache using the
  37. default configuration or the given cache configuration.
  38. The config can be a object of class [net.sf.ehcache.config.CacheConfiguration](http://ehcache.org/apidocs/net/sf/ehcache/config/CacheConfiguration.html) or a clojure map containing keys that correspond to the setters
  39. of the Cache configuration. The keys are converted to camelCase internally
  40. , so for example:
  41. {:max-elements-in-memory 100} calls setMaxElementsInMemory(100)
  42. A CacheManager can also be passed in as the first argument, without this the singleton CacheManager is used (which should be fine for most uses).
  43. ## new-manager [] [config]
  44. Creates a new cache manager. The config can be a filename string, URL object or an InputStream containing an XML configuration. To set the configuration without using an external XML file, a clojure [prxml](http://richhickey.github.com/clojure-contrib/prxml-api.html#clojure.contrib.prxml/prxml) style datastructure can be used.
  45. ### example
  46. (new-manager
  47. [:ehcache
  48. [:disk-store
  49. {:path "java.io.tmpdir/mycaches"}]
  50. [:default-cache
  51. {:max-elements-in-memory 100
  52. :eternal false
  53. :overflow-to-disk true
  54. :disk-persistent false
  55. :memory-store-eviction-policy "LRU"}]])
  56. ## Access to Ehcache objects and features
  57. - add [cache k v]
  58. Adds an item to the given cache and returns the value added.
  59. - lookup [cache k]
  60. Looks up an item in the given cache. Returns a vector [element-exists? value].
  61. - invalidate [cache k]
  62. Invalidates the cache entry with the given key.
  63. - create-config []
  64. Creates a CacheConfiguration object.
  65. The functions below can also be called with a CacheManager as the first argument. If a a CacheManager is not passed in then the singleton CacheManager is used.
  66. - create-cache [cache-name config]
  67. Returns an ehcache Cache object with the given name and config.
  68. - cache-seq []
  69. Returns a sequence containing the names of the currently used caches within a cache manager.
  70. - remove-cache [cache-name]
  71. Removes the cache with the given name.
  72. - shutdown []
  73. Shuts down a cache manager.