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) ⇒ Object
- #append(key, value) ⇒ Object
- #decr(key, amount = 1) ⇒ Object
- #delete(key) ⇒ Object
- #flush_all(delay = nil) ⇒ Object
- #get(keys) ⇒ 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) ⇒ Object
- #set(key, value, expiry = 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) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/memcache/pg_server.rb', line 84 def add(key, value, expiry = 0) delete_expired(key) insert(key, value, expiry) value rescue PGError => e nil end |
#append(key, value) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/memcache/pg_server.rb', line 98 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
64 65 66 |
# File 'lib/memcache/pg_server.rb', line 64 def decr(key, amount = 1) incr(key, -amount) end |
#delete(key) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/memcache/pg_server.rb', line 68 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) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/memcache/pg_server.rb', line 31 def get(keys) 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 FROM #{table} WHERE key IN (#{keys}) AND #{prefix_clause} AND #{expiry_clause} } results = {} db.query(sql).each do |row| results[row['key']] = row['value'] end results end |
#incr(key, amount = 1) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/memcache/pg_server.rb', line 48 def incr(key, amount = 1) transaction do value = get(key) return unless 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
108 109 110 111 112 113 114 115 116 |
# File 'lib/memcache/pg_server.rb', line 108 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) ⇒ Object
92 93 94 95 96 |
# File 'lib/memcache/pg_server.rb', line 92 def replace(key, value, expiry = 0) delete_expired(key) result = update(key, value, expiry) result.cmdtuples == 1 ? value : nil end |
#set(key, value, expiry = 0) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/memcache/pg_server.rb', line 76 def set(key, value, expiry = 0) transaction do delete(key) insert(key, value, expiry) end value end |