Class: Memcache::PGServer
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Attributes inherited from Base
Instance Method Summary collapse
- #add(key, value, expiry = 0, flags = 0) ⇒ Object
- #append(key, value) ⇒ Object
- #decr(key, amount = 1) ⇒ Object
- #delete(key) ⇒ Object
- #flush_all(delay = nil) ⇒ Object
- #get(keys, cas = false) ⇒ Object
- #incr(key, amount = 1) ⇒ Object
-
#initialize(opts) ⇒ PGServer
constructor
A new instance of PGServer.
- #name ⇒ Object
- #prepend(key, value) ⇒ Object
- #replace(key, value, expiry = 0, flags = 0) ⇒ Object
- #set(key, value, expiry = 0, flags = 0) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(opts) ⇒ PGServer
Returns a new instance of PGServer.
15 16 17 18 |
# File 'lib/memcache/pg_server.rb', line 15 def initialize(opts) @table = opts[:table] @db = opts[:db] || ActiveRecord::Base.connection.raw_connection end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
13 14 15 |
# File 'lib/memcache/pg_server.rb', line 13 def db @db end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
13 14 15 |
# File 'lib/memcache/pg_server.rb', line 13 def table @table end |
Instance Method Details
#add(key, value, expiry = 0, flags = 0) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/memcache/pg_server.rb', line 87 def add(key, value, expiry = 0, flags = 0) delete_expired(key) insert(key, value, expiry, flags) value rescue PGError => e nil end |
#append(key, value) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/memcache/pg_server.rb', line 101 def append(key, value) delete_expired(key) result = db.exec %{ UPDATE #{table} SET value = value || #{quote(value)}, updated_at = NOW() WHERE key = #{quote(key)} AND #{prefix_clause} } result.cmdtuples == 1 end |
#decr(key, amount = 1) ⇒ Object
67 68 69 |
# File 'lib/memcache/pg_server.rb', line 67 def decr(key, amount = 1) incr(key, -amount) end |
#delete(key) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/memcache/pg_server.rb', line 71 def delete(key) result = db.exec %{ DELETE FROM #{table} WHERE key = #{quote(key)} AND #{prefix_clause} } result.cmdtuples == 1 end |
#flush_all(delay = nil) ⇒ Object
27 28 29 |
# File 'lib/memcache/pg_server.rb', line 27 def flush_all(delay = nil) db.exec("TRUNCATE #{table}") end |
#get(keys, cas = false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/memcache/pg_server.rb', line 31 def get(keys, cas = false) # cas ignored for now return get([keys])[keys.to_s] unless keys.kind_of?(Array) return {} if keys.empty? keys = keys.collect {|key| quote(key.to_s)}.join(',') sql = %{ SELECT key, value, flags FROM #{table} WHERE key IN (#{keys}) AND #{prefix_clause} AND #{expiry_clause} } results = {} db.query(sql).each do |row| results[row['key']] = {:value => row['value'], :flags => row['flags'].to_i} end results end |
#incr(key, amount = 1) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/memcache/pg_server.rb', line 49 def incr(key, amount = 1) transaction do result = get(key) return unless result value = result[:value] return unless value =~ /^\d+$/ value = value.to_i + amount value = 0 if value < 0 db.exec %{ UPDATE #{table} SET value = #{quote(value)}, updated_at = NOW() WHERE key = #{quote(key)} AND #{prefix_clause} } value end end |
#name ⇒ Object
20 21 22 23 24 25 |
# File 'lib/memcache/pg_server.rb', line 20 def name @name ||= begin db_config = db.instance_variable_get(:@config) "#{db_config[:host]}:#{db_config[:database]}:#{table}" end end |
#prepend(key, value) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/memcache/pg_server.rb', line 111 def prepend(key, value) delete_expired(key) result = db.exec %{ UPDATE #{table} SET value = #{quote(value)} || value, updated_at = NOW() WHERE key = #{quote(key)} AND #{prefix_clause} } result.cmdtuples == 1 end |
#replace(key, value, expiry = 0, flags = 0) ⇒ Object
95 96 97 98 99 |
# File 'lib/memcache/pg_server.rb', line 95 def replace(key, value, expiry = 0, flags = 0) delete_expired(key) result = update(key, value, expiry, flags) result.cmdtuples == 1 ? value : nil end |
#set(key, value, expiry = 0, flags = 0) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/memcache/pg_server.rb', line 79 def set(key, value, expiry = 0, flags = 0) transaction do delete(key) insert(key, value, expiry, flags) end value end |