forked from wycats/moneta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
105 lines (76 loc) · 3.24 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
This Fork
=========
Adds an ActiveRecord and a Monetas adapter to Moneta. The first for when you don't have/need a real key/value store in your infrastructure yet, the second for when you transparently want to replicate values to multiple stores.
Moneta::ActiveRecord
====================
You need a migration:
class AddMonetaTable < ActiveRecord::Migration
# Only needed if you want to keep your cache in a separate database
class TheStore < ActiveRecord::Base
establish_connection "#{Rails.env}_store"
end
def self.connection
TheStore.connection
end
def self.up
create_table 'moneta_store', :id => false do |t|
t.string :key, :null => false
t.text :value
end
if mysql?
execute "alter table moneta_store modify column `key` varchar(255) primary key"
else
execute "alter table moneta_store add primary key (key)"
end
end
def self.down
drop_table 'moneta_store'
end
def self.mysql?
ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'mysql'
end
end
Initialize like this:
store = Moneta::ActiveRecord.new(:connection => ActiveRecord::Base.configurations["#{Rails.env}_store"])
Moneta::Monetas
===============
store = Moneta::Monetas.new(:stores => [
Moneta::Memory.new,
Moneta::BasicFile.new(...)])
Read operations select a random cache, write operations write to all caches.
Moneta: A unified interface for key/value stores
================================================
Moneta provides a standard interface for interacting with various kinds of key/value stores.
Out of the box, it supports:
* File store for xattr
* Basic File Store
* Memcache store
* In-memory store
* The xattrs in a file system
* DataMapper
* S3
* Berkeley DB
* Redis
* SDBM
* Tokyo
* CouchDB
All stores support key expiration, but only memcache supports it natively. All other stores
emulate expiration.
The Moneta API is purposely extremely similar to the Hash API. In order so support an
identical API across stores, it does not support iteration or partial matches, but that
might come in a future release.
The API:
#initialize(options):: options differs per-store, and is used to set up the store
#[](key):: retrieve a key. if the key is not available, return nil
#[]=(key, value):: set a value for a key. if the key is already used, clobber it.
keys set using []= will never expire
#delete(key):: delete the key from the store and return the current value
#key?(key):: true if the key exists, false if it does not
#has_key?(key):: alias for key?
#store(key, value, options):: same as []=, but you can supply an :expires_in option,
which will specify a number of seconds before the key
should expire. In order to support the same features
across all stores, only full seconds are supported
#update_key(key, options):: updates an existing key with a new :expires_in option.
if the key has already expired, it will not be updated.
#clear:: clear all keys in this store