Class: SimpleCache::PgStore

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_cache/pg_store.rb

Constant Summary collapse

Marshal =
::SimpleCache::Marshal
TABLE_NAME =
"simple_cache2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ PgStore

Returns a new instance of PgStore.



10
11
12
13
# File 'lib/simple_cache/pg_store.rb', line 10

def initialize(url)
  expect! url => String
  @db = MicroSql.create(url)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/simple_cache/pg_store.rb', line 8

def path
  @path
end

Instance Method Details

#clearObject



31
32
33
34
# File 'lib/simple_cache/pg_store.rb', line 31

def clear
  return unless @db.tables.include?(table.table_name)
  @db.ask "DELETE FROM #{table.table_name}"
end

#fetch(key, &block) ⇒ Object



19
20
21
22
23
24
# File 'lib/simple_cache/pg_store.rb', line 19

def fetch(key, &block)
  value = table[key]
  return Marshal.unmarshal(value) if value
  return yield(self, key) if block
  nil
end

#store(key, value, ttl = nil) ⇒ Object



26
27
28
29
# File 'lib/simple_cache/pg_store.rb', line 26

def store(key, value, ttl = nil)
  table.update(key, Marshal.marshal(value), ttl)
  value
end

#tableObject



15
16
17
# File 'lib/simple_cache/pg_store.rb', line 15

def table
  @db.key_value_table(TABLE_NAME)
end