| 1 |
require 'benchmark' |
|---|
| 2 |
|
|---|
| 3 |
module ActiveSupport |
|---|
| 4 |
module Cache |
|---|
| 5 |
def self.lookup_store(*store_option) |
|---|
| 6 |
store, *parameters = *([ store_option ].flatten) |
|---|
| 7 |
|
|---|
| 8 |
case store |
|---|
| 9 |
when Symbol |
|---|
| 10 |
store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize) |
|---|
| 11 |
store_class = ActiveSupport::Cache.const_get(store_class_name) |
|---|
| 12 |
store_class.new(*parameters) |
|---|
| 13 |
when nil |
|---|
| 14 |
ActiveSupport::Cache::MemoryStore.new |
|---|
| 15 |
else |
|---|
| 16 |
store |
|---|
| 17 |
end |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
def self.expand_cache_key(key, namespace = nil) |
|---|
| 21 |
expanded_cache_key = namespace ? "#{namespace}/" : "" |
|---|
| 22 |
|
|---|
| 23 |
if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] |
|---|
| 24 |
expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
expanded_cache_key << case |
|---|
| 28 |
when key.respond_to?(:cache_key) |
|---|
| 29 |
key.cache_key |
|---|
| 30 |
when key.is_a?(Array) |
|---|
| 31 |
key.collect { |element| expand_cache_key(element) }.to_param |
|---|
| 32 |
when key.respond_to?(:to_param) |
|---|
| 33 |
key.to_param |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
expanded_cache_key |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
class Store |
|---|
| 41 |
cattr_accessor :logger |
|---|
| 42 |
|
|---|
| 43 |
def initialize |
|---|
| 44 |
end |
|---|
| 45 |
|
|---|
| 46 |
def threadsafe! |
|---|
| 47 |
@mutex = Mutex.new |
|---|
| 48 |
self.class.send :include, ThreadSafety |
|---|
| 49 |
self |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
def fetch(key, options = {}) |
|---|
| 54 |
@logger_off = true |
|---|
| 55 |
if !options[:force] && value = read(key, options) |
|---|
| 56 |
@logger_off = false |
|---|
| 57 |
log("hit", key, options) |
|---|
| 58 |
value |
|---|
| 59 |
elsif block_given? |
|---|
| 60 |
@logger_off = false |
|---|
| 61 |
log("miss", key, options) |
|---|
| 62 |
|
|---|
| 63 |
value = nil |
|---|
| 64 |
seconds = Benchmark.realtime { value = yield } |
|---|
| 65 |
|
|---|
| 66 |
@logger_off = true |
|---|
| 67 |
write(key, value, options) |
|---|
| 68 |
@logger_off = false |
|---|
| 69 |
|
|---|
| 70 |
log("write (will save #{'%.5f' % seconds})", key, nil) |
|---|
| 71 |
|
|---|
| 72 |
value |
|---|
| 73 |
end |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
def read(key, options = nil) |
|---|
| 77 |
log("read", key, options) |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
def write(key, value, options = nil) |
|---|
| 81 |
log("write", key, options) |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
def delete(key, options = nil) |
|---|
| 85 |
log("delete", key, options) |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
def delete_matched(matcher, options = nil) |
|---|
| 89 |
log("delete matched", matcher.inspect, options) |
|---|
| 90 |
end |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
private |
|---|
| 94 |
def log(operation, key, options) |
|---|
| 95 |
logger.debug("Cache #{operation}: #{key}#{options ? " ( |
|---|
| 96 |
end |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
module ThreadSafety |
|---|
| 101 |
def read(key, options = nil) |
|---|
| 102 |
@mutex.synchronize { super } |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
def write(key, value, options = nil) |
|---|
| 106 |
@mutex.synchronize { super } |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
def delete(key, options = nil) |
|---|
| 110 |
@mutex.synchronize { super } |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
def delete_matched(matcher, options = nil) |
|---|
| 114 |
@mutex.synchronize { super } |
|---|
| 115 |
end |
|---|
| 116 |
end |
|---|
| 117 |
end |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
require 'active_support/cache/file_store' |
|---|
| 121 |
require 'active_support/cache/memory_store' |
|---|
| 122 |
require 'active_support/cache/drb_store' |
|---|
| 123 |
require 'active_support/cache/mem_cache_store' |
|---|
| 124 |
require 'active_support/cache/compressed_mem_cache_store' |
|---|