Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support standalone ActiveSupport::Cache usage of Ehcache

  * Further improve Rails compatibility
  * Move Rails specific code to lib/ehcache.rb
  * Move ehcache_rails_common code to a base ActiveSupportStore class
  * ActiveSupport cache implementations inherit from ActiveSupportStore
  * Remove rails/ehcache_rails_common.rb
  * Move ActiveSupport 2.x store under active_support/
  * Modify gemspec to reflect current file requirements

These changes allow ehcache to be used with the ActiveSupport cache
implementation outside of Rails. The Ehcache::ActiveSupportStore base class
implements .config_directory and .default_cache_name to give non-rails
projects access to these settings before the cache store is configured. The
file `ehcache.rb` now houses the Rails specific configuration which simplifies
usage and makes the special rails gems mostly superfluous (See FileList in the
jeweler raketask.)
  • Loading branch information...
commit 7888ec5115336f12c444c04ca9473485f6b1b8b8 1 parent ae0ef1c
@elskwid elskwid authored
View
4 Rakefile
@@ -42,7 +42,7 @@ Jeweler::Tasks.new do |gemspec|
gemspec.name = 'jruby-ehcache-rails3'
gemspec.summary = 'Rails 3 cache store provider using Ehcache'
gemspec.description = 'Rails 3 cache store provider using Ehcache'
- gemspec.files = FileList['lib/active_support/**/*', 'lib/ehcache/rails/**/*']
+ gemspec.files = FileList['lib/active_support/**/*', 'lib/ehcache/active_support_store.rb']
gemspec.test_files = []
gemspec.add_dependency 'jruby-ehcache', ">=1.1.2"
end
@@ -52,7 +52,7 @@ Jeweler::Tasks.new do |gemspec|
gemspec.name = 'jruby-ehcache-rails2'
gemspec.summary = 'Rails 2 cache store provider using Ehcache'
gemspec.description = 'Rails 2 cache store provider using Ehcache'
- gemspec.files = FileList['lib/active_support/**/*', 'lib/ehcache/rails/**/*']
+ gemspec.files = FileList['lib/active_support/**/*', 'lib/ehcache/active_support_store.rb']
gemspec.test_files = []
gemspec.add_dependency 'jruby-ehcache', ">=1.1.2"
end
View
0  bin/ehcache 100644 → 100755
File mode changed
View
13 lib/active_support/cache/ehcache_store.rb
@@ -1,19 +1,18 @@
-require 'ehcache/rails/ehcache_rails_common'
+require 'ehcache/active_support_store'
+
+# Rails 3 cache store implementation which stores data in Ehcache:
+# http://www.ehcache.org/
module ActiveSupport
module Cache
-
- # Rails 3 cache store implementation which stores data in Ehcache:
- # http://www.ehcache.org/
- class EhcacheStore < Store
- include Ehcache::Rails
+ class EhcacheStore < Ehcache::ActiveSupportStore
def initialize(*args)
args = args.flatten
options = args.extract_options!
super(options)
self.create_cache_manager(options)
- @ehcache = self.create_cache(options) # This comes from the Ehcache::Rails mixin.
+ @ehcache = self.create_cache(options)
extend Strategy::LocalCache
end
View
13 lib/ehcache_store.rb → lib/active_support/ehcache_store.rb
@@ -1,16 +1,15 @@
-require 'activesupport'
-require 'ehcache/rails/ehcache_rails_common'
+require 'ehcache/active_support_store'
+
+# Rails 2 cache store implementation which stores data in Ehcache:
+# http://www.ehcache.org/
module ActiveSupport
module Cache
- # Rails 2 cache store implementation which stores data in Ehcache:
- # http://www.ehcache.org/
- class EhcacheStore < Store
- include Ehcache::Rails
+ class EhcacheStore < Ehcache::ActiveSupportStore
def initialize(options = {})
super() # Rails 2.3.x Store doesn't take any arguments to initialize
- @ehcache = self.create_cache # This comes from the Ehcache::Rails mixin.
+ @ehcache = self.create_cache
end
def read(key, options = nil)
View
29 lib/ehcache.rb
@@ -1,6 +1,6 @@
unless $:.include?(File.dirname(__FILE__)) ||
$:.include?(File.expand_path(File.dirname(__FILE__)))
- $:.unshift(File.dirname(__FILE__))
+ $:.unshift(File.dirname(__FILE__))
end
require 'java'
@@ -20,3 +20,30 @@ class EhcacheError < RuntimeError; end
require 'ehcache/cache'
require 'ehcache/cache_manager'
require 'ehcache/element'
+
+if defined?(Rails)
+ require 'ehcache/active_support_store'
+
+ case Rails::VERSION::MAJOR
+ when 2
+ require 'active_support/ehcache_store' # AS 2 impl
+
+ ActiveSupport::Cache::EhcacheStore.config_directory = File.expand_path(File.join(RAILS_ROOT, 'config'))
+ ActiveSupport::Cache::EhcacheStore.default_cache_name = 'rails_cache'
+
+ when 3
+ require 'active_support/cache/ehcache_store' # AS 3 impl
+
+ # Railtie
+ module Ehcache
+ class Railtie < ::Rails::Railtie
+ initializer "ehcache.setup_paths" do
+ ActiveSupport::Cache::EhcacheStore.config_directory = File.expand_path(File.join(::Rails.root, 'config'))
+ ActiveSupport::Cache::EhcacheStore.default_cache_name = 'rails_cache'
+ end
+ end
+ end
+
+ end
+
+end
View
47 lib/ehcache/active_support_store.rb
@@ -0,0 +1,47 @@
+# Common code used in the ehcache_store implementations for ActiveSupport 2.x and 3.x
+require 'active_support'
+
+begin
+ require 'ehcache'
+rescue LoadError => e
+ $stderr.puts "You don't have ehcache installed in your application."
+ $stderr.puts "Please add it to your Gemfile and run bundle install"
+ raise e
+end
+
+# Base class for both the ActiveSupport 2 & 3 implementations
+module Ehcache
+ class ActiveSupportStore < ActiveSupport::Cache::Store
+
+ cattr_accessor :config_directory
+ cattr_accessor :default_cache_name
+
+ attr_reader :cache_manager
+
+ def create_cache_manager(options = {})
+ config_dir = self.class.config_directory
+ config = if options[:ehcache_config]
+ File.expand_path(File.join(config_dir, options[:ehcache_config]))
+ else
+ Ehcache::Config::Configuration.find(config_dir) if config_dir
+ end
+ # Ehcache will use the failsafe configuration if nothing is passed in
+ # Note: .create is a factory method
+ @cache_manager = Ehcache::CacheManager.create(config)
+ end
+
+ def create_cache(options = {})
+ create_cache_manager(options) if @cache_manager.nil?
+ @cache_manager.cache(options[:cache_name] || default_cache_name)
+ end
+
+ def default_cache_name
+ self.class.default_cache_name || 'app_cache'
+ end
+
+ at_exit do
+ @cache_manager.shutdown if @cache_manager
+ end
+
+ end
+end
View
42 lib/ehcache/rails/ehcache_rails_common.rb
@@ -1,42 +0,0 @@
-begin
- require 'ehcache'
-rescue LoadError => e
- $stderr.puts "You don't have ehcache installed in your application."
- $stderr.puts "Please add it to your Gemfile and run bundle install"
- raise e
-end
-
-module Ehcache
- # Mixin module providing facilities for the Rails 2 and Rails 3 Cache::Store
- # implementations.
- module Rails
- root = defined?(::Rails.root) ? ::Rails.root : RAILS_ROOT
- RAILS_CONFIG_DIR = File.join(root, 'config')
-
- DEFAULT_RAILS_CACHE_NAME = 'rails_cache'
-
- attr_reader :cache_manager
-
- def create_cache_manager(options = {})
- config = nil
- if options[:ehcache_config]
- Dir.chdir(RAILS_CONFIG_DIR) do
- config = File.expand_path(options[:ehcache_config])
- end
- else
- config = Ehcache::Config::Configuration.find(RAILS_CONFIG_DIR)
- end
- @cache_manager = Ehcache::CacheManager.create(config)
- end
-
- def create_cache(options = {})
- create_cache_manager(options) if @cache_manager.nil?
-
- @cache = @cache_manager.cache(options[:cache_name] || DEFAULT_RAILS_CACHE_NAME)
- end
-
- at_exit do
- @cache_manager.shutdown if @cache_manager
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.