Permalink
Browse files

added ehcache_store, and simplified rails install

  • Loading branch information...
1 parent d2255a9 commit 0a4926244c1f9601e4167ba8b703311b29c7ef5e Dylan Stamat committed Jul 2, 2008
View
@@ -4,7 +4,7 @@ Manifest.txt
PostInstall.txt
README.txt
Rakefile
-bin/ehcache_rails
+bin/ehcache
config/ehcache.yml
config/hoe.rb
config/requirements.rb
@@ -13,16 +13,16 @@ ext/ehcache-1.4.1/ehcache-1.4.1.jar
ext/ehcache-1.4.1/lib/backport-util-concurrent-3.0.jar
ext/ehcache-1.4.1/lib/commons-logging-1.0.4.jar
ext/ehcache-1.4.1/lib/jsr107cache-1.0.jar
+ext/rails/ehcache_store.rb
lib/ehcache.rb
lib/ehcache/cache.rb
lib/ehcache/cache_manager.rb
lib/ehcache/config.rb
lib/ehcache/element.rb
lib/ehcache/extensions.rb
lib/ehcache/java.rb
+lib/ehcache/status.rb
lib/ehcache/version.rb
-lib/ehcache_rails/init.rb
-lib/ehcache_rails/lib/ehcache_rails/ehcache_store.rb
script/console
script/destroy
script/generate
View
@@ -1,42 +1,83 @@
-= ehcache
+= Ehcache for Ruby
+
+http://github.com/dylanz/ehcache
-* FIX (url)
== DESCRIPTION:
-Ehcache is a simplified JRuby interface to Java's Ehcache.
-Simplified, meaning no Singleton support, and a ton of other small
-things left out primarily because I didn't need them :P
+Ehcache is a simplified JRuby interface to Java's (JSR(107) compliant) Ehcache.
+Simplified meaning that it should work out-of-the-box, but a lot of native
+methods haven't been interfaced yet, as they weren't needed. Configuration
+occurs in config/ehcache.yml, and should support all the configuration
+options available.
+
+Some biased and non-biased Ehcache VS Memcache articles:
+http://gregluck.com/blog/archives/2007/05/comparing_memca.html
+http://feedblog.org/2007/05/21/unfair-benchmarks-of-ehcache-vs-memcached
+http://blog.aristotlesdog.com/2008/05/01/memcached_vs_ehcache/
+http://www.hugotroche.com/my_weblog/2008/06/ehcache-vs-memc.html
-A complete inteface would be grand, and contribution is welcome !
+For more information on Ehcache, see:
+http://ehcache.sourceforge.net/
-== FEATURES/PROBLEMS:
+Configuration, Code Samples and everything else, see:
+http://ehcache.sourceforge.net/EhcacheUserGuide.html
-No Singleton support, and... probably more.
-== SYNOPSIS:
+== BASIC USAGE:
manager = CacheManager.new
-cache = manager.cache("cache")
-cache.put("key", "value")
+cache = manager.cache
+cache.put("key", "value", {:ttl => 120})
cache.get("key")
-
manager.shutdown
-TODO: Rails support
-Run the ehcache_rails command from your projects root. This will
-install the needed ehcache_rails plugin, which adds the required
-EhcacheStore cache store, and other rails'isms.
+== RAILS:
+
+An EhcacheStore is available for use within Rails, so all the native
+Rails caching methods are supported. Make sure your config/environments/*,
+are setup to support caching, eg: config.action_controller.perform_caching = true
+
+1) From your RAILS_ROOT, run this command:
+ - ehcache rails ## just copies ehcache_store.rb into lib/ at the moment
+
+2) In your environment.rb, specify:
+ - config.cache_store = :ehcache_store
+
+3) Cache stuff
== REQUIREMENTS:
-* FIX (list of requirements)
+Tested with JRuby 1.1.2.
+
== INSTALL:
sudo gem install ehcache
+
== LICENSE:
-...
+Copyright (c) 2008 Dylan Stamat <dstamat@elctech.com>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,20 @@
+#!/usr/bin/env ruby
+
+require 'rake'
+require 'rubygems'
+require 'ehcache'
+
+# TODO: optionparser this up once anything else is added
+if $*.pop == "rails"
+ unless File.exists?("config/boot.rb")
+ abort "Oops... make sure you are running this from your RAILS_ROOT"
+ end
+
+ if File.exists?("lib/ehcache_store.rb")
+ warn "It looks like ehcache_store.rb already exists in lib/"
+ else
+ FileUtils.cp("#{Ehcache::EHCACHE_HOME}/ext/rails/ehcache_store.rb", "lib/")
+ end
+else
+ warn "Usage: specify 'rails' as an argument to install required files"
+end
View
@@ -1,16 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'rake'
-require 'rubygems'
-require 'ehcache'
-
-
-unless File.exists?("config/boot.rb")
- abort "Oops... make sure you are running this from your RAILS_ROOT"
-end
-
-if File.exists?("vendor/plugins/ehcache_rails")
- warn "It looks like ehcache_rails already exists !"
-else
- FileUtils.cp_r("#{Ehcache::EHCACHE_HOME}/lib/ehcache_rails", "vendor/plugins/")
-end
View
@@ -12,8 +12,8 @@ peer_listener:
default:
name: cache
max_elements_in_memory: 10000
- time_to_live_seconds: 120
- time_to_idle_seconds: 120
+ time_to_live_seconds: 0
+ time_to_idle_seconds: 0
overflow_to_disk: true
eternal: false
disk_spool_buffer_size_mb: 30
View
@@ -0,0 +1,59 @@
+require 'activesupport'
+require 'ehcache'
+
+module ActiveSupport
+ module Cache
+ class EhcacheStore < Store
+ def initialize(options = {})
+ manager = Ehcache::CacheManager.new(options)
+ @data = manager.cache
+ end
+
+ def read(key, options = nil)
+ super
+ @data.get(key)
+ rescue Ehcache::EhcacheError => e
+ logger.error("EhcacheError (#{e}): #{e.message}")
+ false
+ end
+
+ def write(key, value, options = nil)
+ super
+ @data.set(key, value, options)
+ true
+ rescue Ehcache::EhcacheError => e
+ logger.error("MemCacheError (#{e}): #{e.message}")
+ false
+ end
+
+ def delete(key, options = nil)
+ super
+ @data.delete(key)
+ rescue Exception => e
+ logger.error("MemCacheError (#{e}): #{e.message}")
+ false
+ end
+
+ def keys
+ @data.keys
+ end
+
+ def exist?(key, options = nil)
+ @data.exist?(key)
+ end
+
+ def delete_matched(matcher, options = nil)
+ super
+ raise "Not supported by Ehcache"
+ end
+
+ def clear
+ @data.clear
+ true
+ rescue Exception => e
+ logger.error("EhcacheError (#{e}): #{e.message}")
+ false
+ end
+ end
+ end
+end
View
@@ -7,6 +7,9 @@ module Ehcache
unless defined?(EHCACHE_HOME)
EHCACHE_HOME = File.expand_path(File.dirname(__FILE__) + '/..')
end
+
+ # wraps all native exceptions
+ class EhcacheError < RuntimeError; end
end
require 'ehcache/java'
@@ -16,3 +19,4 @@ module Ehcache
require 'ehcache/element'
require 'ehcache/extensions'
require 'ehcache/version'
+require 'ehcache/status'
View
@@ -4,57 +4,116 @@ class Cache
# pull cache from given manager by name
def initialize(manager, cache_name)
- @cache = manager.get_cache(cache_name)
+ @proxy = manager.get_cache(cache_name)
end
# put a new element into the cache
- def put(key, value)
- @cache.put(Ehcache::Java::Element.new(key, value))
+ 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
# get an element value from cache by key
def get(key)
- element = @cache.get(key)
+ element = @proxy.get(key)
element ? element.get_value : nil
+ rescue NativeException => e
+ raise EhcacheError, e.cause
end
# get an element from cache by key
def element(key)
- element = @cache.get(key)
+ element = @proxy.get(key)
return nil unless element
- Ehcache::Element.new(element.get_key, element.get_value)
+ Ehcache::Element.new(element.get_key, element.get_value,
+ {:ttl => element.get_time_to_live })
+ rescue NativeException => e
+ raise EhcacheError, e.cause
+ end
+
+ # remove an element from the cache by key
+ def remove(key)
+ @proxy.remove(key)
+ rescue NativeException => e
+ raise EhcacheError, e.cause
+ end
+
+ # 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
- @cache.get_size
+ @proxy.get_size
end
# number of elements in the memory store
def memory_size
- @cache.get_memory_store_size
+ @proxy.get_memory_store_size
end
# number of elements in the cache store
def disk_size
- @cache.get_disk_store_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
- @cache.get_max_elements_in_memory
+ @proxy.get_max_elements_in_memory
end
def eternal?
- @cache.is_eternal
+ @proxy.is_eternal
end
def ttl
- @cache.get_time_to_live_seconds
+ @proxy.get_time_to_live_seconds
end
def tti
- @cache.get_time_to_idle_seconds
+ @proxy.get_time_to_idle_seconds
end
end
end
@@ -1,4 +1,3 @@
-# TODO statuses
module Ehcache
class CacheManager
def initialize(options={})
Oops, something went wrong.

0 comments on commit 0a49262

Please sign in to comment.