lib/ehcache/cache.rb in jruby-ehcache-0.5.0 vs lib/ehcache/cache.rb in jruby-ehcache-1.0.0

- old
+ new

@@ -1,127 +1,58 @@ -module Ehcache - class Cache - PRIMARY = "cache" +# Enhance net.sf.ehcache.Cache with a more Rubyesque API. +class Java::NetSfEhcache::Cache + include Enumerable - # pull cache from given manager by name - def initialize(manager, cache_name) - @proxy = manager.get_cache(cache_name) + # Yield each Element stored in this cache to the given block. This method + # uses Cache#getKeys as its basis, and therefore it is possible that some + # of the yielded elements have expired. + def each + for key in self.getKeys + yield self.get(key) end + end + # Gets an element value from the cache. Unlike the #get method, this method + # returns the element value, not the Element object. + def [](key) + element = self.get(key) + element ? element.value : nil + end - # put a new element into the cache - def put(key, value, options = {}) - if key.nil? || key.empty? - raise EhcacheError, "Element cannot be blank" - end - element = Ehcache::Element.new(key, value, options) - @proxy.put(element.proxy) - rescue NativeException => e - raise EhcacheError, e.cause - end - alias_method :set, :put - alias_method :add, :put + alias ehcache_put put - # another alias for put - def []=(key, value) - put(key, value) + # Wrap the Cache#put method to allow for extra options to be passed to the + # created Element. + def put(*args) + options = args.extract_options! + if args.size == 1 && args.first.kind_of?(Ehcache::Element) + element = args.first + elsif args.size == 2 + element = Ehcache::Element.create(args[0], args[1], options) + else + raise ArgumentError, "Must be Element object or key and value arguments" end + ehcache_put(element) + end - # get an element value from cache by key - def get(key) - element = @proxy.get(key) - element ? element.get_value : nil - rescue NativeException => e - raise EhcacheError, e.cause - end - alias_method :[], :get + alias []= put - # get an element from cache by key - def element(key) - element = @proxy.get(key) - return nil unless element - Ehcache::Element.new(element.get_key, element.get_value, - {:ttl => element.get_time_to_live }) - rescue NativeException => e - raise EhcacheError, e.cause - end + alias include? isKeyInCache + alias member? isKeyInCache - # remove an element from the cache by key - def remove(key) - @proxy.remove(key) - rescue NativeException => e - raise EhcacheError, e.cause - end - alias_method :delete, :remove - - # remove all elements from the cache - def remove_all - @proxy.remove_all - rescue NativeException => e - raise EhcacheError, e.cause - end - alias_method :clear, :remove_all - - def keys - @proxy.get_keys - end - - def exist?(key) - @proxy.is_key_in_cache(key) - end - - # returns the current status of the cache - def status - @proxy.get_status - end - - def alive? - @proxy.get_status == Status::ALIVE - end - - def shutdown? - @proxy.get_status == Status::SHUTDOWN - end - - def uninitialized? - @proxy.get_status == Status::UNINITIALISED - end - - # number of elements in the cache - def size - @proxy.get_size - end - - # number of elements in the memory store - def memory_size - @proxy.get_memory_store_size - end - - # number of elements in the cache store - def disk_size - @proxy.get_disk_store_size - end - - # TODO: implement statistics ! - # return statistics about the cache - def statistics - @proxy.get_statistics - rescue NativeException => e - raise EhcacheError, e.cause - end - - def max_elements - @proxy.get_max_elements_in_memory - end - - def eternal? - @proxy.is_eternal - end - - def ttl - @proxy.get_time_to_live_seconds - end - - def tti - @proxy.get_time_to_idle_seconds - end + # Atomic compare and swap for cache elements. Invokes the given block with + # the current value of the element and attempts to replace it with the + # value returned from the block, repeating until replace returns true. + # Note that the provided block works only with element values, not Element + # objects: the result of element.getValue is passed to the block parameter, + # and the block is expected to return a value based on it. + # If there is no element with the given key, returns immediately without + # retrying. + def compare_and_swap(key, &block) + begin + old_element = self.get(key) + return nil unless old_element + new_element = Ehcache::Element.new(key, yield(old_element.value)) + end until replace(old_element, new_element) end + + alias update compare_and_swap end