Class: MemCache::ActiveRecord
- Inherits:
-
Object
- Object
- MemCache::ActiveRecord
- Defined in:
- lib/memcache/activerecord.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :autofix_keys => false, :check_size => true, :failover => false, :logger => nil, :multithread => true, :namespace => nil, :namespace_separator => ':', :no_reply => false, :readonly => false, :timeout => nil, }
- MAX_KEY_SIZE =
250
- MAX_VALUE_SIZE =
2 ** 20
- THIRTY_DAYS =
60 * 60 * 24 * 30
- COLUMN_NAMES =
{ :key => 'key', :value => 'value', :cas => 'cas', :expiry => 'expiry', }
- STORED =
"STORED\r\n"
- NOT_STORED =
"NOT_STORED\r\n"
- EXISTS =
"EXISTS\r\n"
- DELETED =
"DELETED\r\n"
- NOT_FOUND =
"NOT_FOUND\r\n"
Instance Attribute Summary collapse
-
#autofix_keys ⇒ Object
readonly
Returns the value of attribute autofix_keys.
-
#failover ⇒ Object
readonly
Returns the value of attribute failover.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#multithread ⇒ Object
readonly
Returns the value of attribute multithread.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#no_reply ⇒ Object
readonly
Returns the value of attribute no_reply.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #active? ⇒ Boolean
- #add(key, value, expiry = 0, raw = false) ⇒ Object
- #append(key, value) ⇒ Object
- #cas(key, expiry = 0, raw = false, &block) ⇒ Object
- #decr(key, amount = 1) ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key, expiry = 0, raw = false, &block) ⇒ Object
- #flush_all ⇒ Object
- #garbage_collection! ⇒ Object
- #get(key, raw = false) ⇒ Object (also: #[])
- #get_multi(*keys) ⇒ Object
- #incr(key, amount = 1) ⇒ Object
-
#initialize(active_record, options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #inspect ⇒ Object
- #prepend(key, value) ⇒ Object
- #readonly? ⇒ Boolean
- #replace(key, value, expiry = 0, raw = false) ⇒ Object
- #set(key, value, expiry = 0, raw = false) ⇒ Object
Constructor Details
#initialize(active_record, options = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/memcache/activerecord.rb', line 43 def initialize(active_record, = {}) @ar = active_record [ :check_size, :failover, :logger, :multithread, :timeout, ].each do |name| if .key?(name) && [name] != DEFAULT_OPTIONS[name] raise ArgumentError, "#{name} isn't changeable" end end = DEFAULT_OPTIONS.merge() @autofix_keys = [:autofix_keys] @check_size = [:check_size] @failover = [:failover] @logger = [:logger] @multithread = [:multithread] @namespace = [:namespace] @namespace_separator = [:namespace_separator] @no_reply = [:no_reply] @readonly = [:readonly] @timeout = [:timeout] end |
Instance Attribute Details
#autofix_keys ⇒ Object (readonly)
Returns the value of attribute autofix_keys.
35 36 37 |
# File 'lib/memcache/activerecord.rb', line 35 def autofix_keys @autofix_keys end |
#failover ⇒ Object (readonly)
Returns the value of attribute failover.
36 37 38 |
# File 'lib/memcache/activerecord.rb', line 36 def failover @failover end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
37 38 39 |
# File 'lib/memcache/activerecord.rb', line 37 def logger @logger end |
#multithread ⇒ Object (readonly)
Returns the value of attribute multithread.
38 39 40 |
# File 'lib/memcache/activerecord.rb', line 38 def multithread @multithread end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
39 40 41 |
# File 'lib/memcache/activerecord.rb', line 39 def namespace @namespace end |
#no_reply ⇒ Object (readonly)
Returns the value of attribute no_reply.
40 41 42 |
# File 'lib/memcache/activerecord.rb', line 40 def no_reply @no_reply end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
41 42 43 |
# File 'lib/memcache/activerecord.rb', line 41 def timeout @timeout end |
Instance Method Details
#[]=(key, value) ⇒ Object
212 213 214 |
# File 'lib/memcache/activerecord.rb', line 212 def []=(key, value) set(key, value) end |
#active? ⇒ Boolean
76 77 78 |
# File 'lib/memcache/activerecord.rb', line 76 def active? true end |
#add(key, value, expiry = 0, raw = false) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/memcache/activerecord.rb', line 136 def add(key, value, expiry = 0, raw = false) check_readonly! cache_key = make_cache_key(key) value = value_to_storable(value, raw) old_value, old_expiry = find(__method__, cache_key, [:value, :expiry], false) if old_value && available?(old_expiry) NOT_STORED unless @no_reply else if old_value update(__method__, cache_key, value, expiry) else # rescue duplicate key error insert(__method__, cache_key, value, expiry) rescue nil end STORED unless @no_reply end end |
#append(key, value) ⇒ Object
173 174 175 |
# File 'lib/memcache/activerecord.rb', line 173 def append(key, value) append_or_prepend(__method__, key, value) end |
#cas(key, expiry = 0, raw = false, &block) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/memcache/activerecord.rb', line 128 def cas(key, expiry = 0, raw = false, &block) check_readonly! raise MemCacheError, 'A block is required' unless block_given? result = cas_with_reply(__method__, key, expiry, raw, &block) result unless @no_reply end |
#decr(key, amount = 1) ⇒ Object
185 186 187 |
# File 'lib/memcache/activerecord.rb', line 185 def decr(key, amount = 1) incr_or_decl(__method__, key, amount) end |
#delete(key) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/memcache/activerecord.rb', line 189 def delete(key) check_readonly! cache_key = make_cache_key(key) conditions = { COLUMN_NAMES[:key] => cache_key } if @no_reply _delete(__method__, conditions) nil else exists = !!find(__method__, cache_key, :key, true) _delete(__method__, conditions) exists ? DELETED : NOT_FOUND end end |
#fetch(key, expiry = 0, raw = false, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/memcache/activerecord.rb', line 91 def fetch(key, expiry = 0, raw = false, &block) value = get(key, raw) if value.nil? && block_given? value = yield add(key, value, expiry, raw) end value end |
#flush_all ⇒ Object
205 206 207 208 |
# File 'lib/memcache/activerecord.rb', line 205 def flush_all check_readonly! truncate(__method__) end |
#garbage_collection! ⇒ Object
216 217 218 |
# File 'lib/memcache/activerecord.rb', line 216 def garbage_collection! _delete(__method__, ["#{quote_column_name(:expiry)} <= ?", now]) end |
#get(key, raw = false) ⇒ Object Also known as: []
84 85 86 87 88 89 |
# File 'lib/memcache/activerecord.rb', line 84 def get(key, raw = false) cache_key = make_cache_key(key) if value = find(__method__, cache_key, :value, true) raw ? value : Marshal.load(value) end end |
#get_multi(*keys) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/memcache/activerecord.rb', line 102 def get_multi(*keys) cache_keys = keys.inject({}) do |cache_keys, key| cache_keys[make_cache_key(key)] = key cache_keys end rows = find_all(__method__, cache_keys.keys, [:key, :value], true) rows.inject({}) do |hash, (key, value)| hash[cache_keys[key]] = Marshal.load(value) hash end end |
#incr(key, amount = 1) ⇒ Object
181 182 183 |
# File 'lib/memcache/activerecord.rb', line 181 def incr(key, amount = 1) incr_or_decl(__method__, key, amount) end |
#inspect ⇒ Object
71 72 73 74 |
# File 'lib/memcache/activerecord.rb', line 71 def inspect '<%s: %s, ns: %p, ro: %p>' % [self.class, @ar, @namespace, @readonly] end |
#prepend(key, value) ⇒ Object
177 178 179 |
# File 'lib/memcache/activerecord.rb', line 177 def prepend(key, value) append_or_prepend(__method__, key, value) end |
#readonly? ⇒ Boolean
80 81 82 |
# File 'lib/memcache/activerecord.rb', line 80 def readonly? @readonly end |
#replace(key, value, expiry = 0, raw = false) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/memcache/activerecord.rb', line 160 def replace(key, value, expiry = 0, raw = false) check_readonly! cache_key = make_cache_key(key) value = value_to_storable(value, raw) if update(__method__, cache_key, value, expiry, true) STORED unless @no_reply else NOT_STORED unless @no_reply end end |
#set(key, value, expiry = 0, raw = false) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/memcache/activerecord.rb', line 114 def set(key, value, expiry = 0, raw = false) check_readonly! cache_key = make_cache_key(key) value = value_to_storable(value, raw) unless update(__method__, cache_key, value, expiry) # rescue duplicate key error insert(__method__, cache_key, value, expiry) rescue nil end STORED unless @no_reply end |