Skip to content

Commit

Permalink
added initial rails support... broken, and to be fixed.
Browse files Browse the repository at this point in the history
will allow an :ehcache_store option to cache_store=
  • Loading branch information
Dylan Stamat committed Jul 1, 2008
1 parent 87cbaf5 commit d2255a9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Manifest.txt
Expand Up @@ -4,6 +4,7 @@ Manifest.txt
PostInstall.txt
README.txt
Rakefile
bin/ehcache_rails
config/ehcache.yml
config/hoe.rb
config/requirements.rb
Expand All @@ -20,6 +21,8 @@ lib/ehcache/element.rb
lib/ehcache/extensions.rb
lib/ehcache/java.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
Expand Down
7 changes: 7 additions & 0 deletions README.txt
Expand Up @@ -23,6 +23,13 @@ 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.


== REQUIREMENTS:

* FIX (list of requirements)
Expand Down
16 changes: 16 additions & 0 deletions bin/ehcache_rails
@@ -0,0 +1,16 @@
#!/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
2 changes: 2 additions & 0 deletions lib/ehcache.rb
@@ -1,6 +1,8 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'java'

module Ehcache
unless defined?(EHCACHE_HOME)
EHCACHE_HOME = File.expand_path(File.dirname(__FILE__) + '/..')
Expand Down
1 change: 1 addition & 0 deletions lib/ehcache_rails/init.rb
@@ -0,0 +1 @@
require 'ehcache_rails/ehcache_store'
103 changes: 103 additions & 0 deletions lib/ehcache_rails/lib/ehcache_rails/ehcache_store.rb
@@ -0,0 +1,103 @@
require 'ehcache'

module ActiveSupport
module Cache
class EhcacheStore < Store
module Response
STORED = "STORED\r\n"
NOT_STORED = "NOT_STORED\r\n"
EXISTS = "EXISTS\r\n"
NOT_FOUND = "NOT_FOUND\r\n"
DELETED = "DELETED\r\n"
end

#attr_reader :addresses

def initialize(*addresses)
puts "INITIALIZING EHCACHE STORE"
#addresses = addresses.flatten
#options = addresses.extract_options!
#addresses = ["localhost"] if addresses.empty?
#@addresses = addresses
#@data = MemCache.new(addresses, options)
manager = Ehcache::CacheManager.new
@cache = manager.cache('cache')
end

def read(key, options = nil)
super
@data.get(key, raw?(options))
rescue Exception => e
logger.error("MemCacheError (#{e}): #{e.message}")
nil
end

# Set key = value. Pass :unless_exist => true if you don't
# want to update the cache if the key is already set.
def write(key, value, options = nil)
super
method = options && options[:unless_exist] ? :add : :set
response = @data.send(method, key, value, expires_in(options), raw?(options))
response == Response::STORED
rescue Exception => e
logger.error("MemCacheError (#{e}): #{e.message}")
false
end

def delete(key, options = nil)
super
response = @data.delete(key, expires_in(options))
response == Response::DELETED
rescue Exception => e
logger.error("MemCacheError (#{e}): #{e.message}")
false
end

def exist?(key, options = nil)
# Doesn't call super, cause exist? in memcache is in fact a read
# But who cares? Reading is very fast anyway
!read(key, options).nil?
end

def increment(key, amount = 1)
log("incrementing", key, amount)

response = @data.incr(key, amount)
response == Response::NOT_FOUND ? nil : response
rescue Exception
nil
end

def decrement(key, amount = 1)
log("decrement", key, amount)

response = data.decr(key, amount)
response == Response::NOT_FOUND ? nil : response
rescue Exception
nil
end

def delete_matched(matcher, options = nil)
super
raise "Not supported by Memcache"
end

def clear
@data.flush_all
end

def stats
@data.stats
end

private
def expires_in(options)
(options && options[:expires_in]) || 0
end

def raw?(options)
options && options[:raw]
end
end
end
end
Binary file modified pkg/ehcache-0.0.1.gem
Binary file not shown.

0 comments on commit d2255a9

Please sign in to comment.