Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added initial rails support... broken, and to be fixed.
will allow an :ehcache_store option to cache_store=
- Loading branch information
Dylan Stamat
committed
Jul 1, 2008
1 parent
87cbaf5
commit d2255a9
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'ehcache_rails/ehcache_store' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.