sir_cachealot 0.5 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +26 -166
- data/README.md +112 -16
- data/Rakefile +1 -1
- data/lib/sir.rb +145 -0
- data/lib/sir/backends.rb +8 -0
- data/lib/sir/backends/Base.rb +154 -0
- data/lib/sir/backends/ram_cache.rb +152 -0
- data/lib/sir/backends/redis_cache.rb +145 -0
- data/lib/sir/version.rb +3 -0
- data/lib/sircachealot.rb +1 -0
- data/sir_cachealot.gemspec +13 -6
- data/spec/redis_spec.rb +84 -31
- data/spec/sir_cachealot_spec.rb +68 -69
- data/spec/spec_helper.rb +1 -1
- metadata +32 -11
- data/lib/sir_cachealot.rb +0 -282
- data/lib/sir_cachealot/version.rb +0 -3
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
OTNmODJhZjRhODc5OGMyOTllMDE0MzQ2NmQ0ZWEzZTgzYmQ2NTMwZA==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
NzY0NjQ1ZTZmMWM5ZDhlYmI2MGMyNjhiNjkyOTZlNTY1NDE1MDY5Mg==
|
|
7
7
|
!binary "U0hBNTEy":
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
ODgzYWQ0MDViZDkyN2UwMjYwMTAxZTAzZTc3OGMxODU5YmEwODQ2ZDY2Njhh
|
|
10
|
+
YTNjNmU4NDcyZGQzNDIzYmZhODM1MjI4N2Y2YzA3YzA1MTRhNmYyYzA0YmVm
|
|
11
|
+
ZGMxYWVlNzRlNDA3MmE1MTM2MDNiYTE1ODZlMTNhOGEwY2IzODk=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
ZmUxNWJiMGRkNGZiNTQwZjBjNjFiOGJiNjllNTQ0NGMxNWFiZDQxZjIwZWNm
|
|
14
|
+
ODExYTM2OGIxZDQ2ZDYzM2RkZjVjMTc2YzY1MWVhOTE4NzA5ZmIxYmExN2Ez
|
|
15
|
+
NDc4MDNkY2ZiODgwNWM4YmI5YTg2MGFhMDQwNWYxZWI4OGQ2ZDY=
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
|
@@ -1,166 +1,26 @@
|
|
|
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
|
-
of
|
|
26
|
-
|
|
27
|
-
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
-
Application with the Library. The particular version of the Library
|
|
29
|
-
with which the Combined Work was made is also called the "Linked
|
|
30
|
-
Version".
|
|
31
|
-
|
|
32
|
-
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
-
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
-
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
-
based on the Application, and not on the Linked Version.
|
|
36
|
-
|
|
37
|
-
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
-
object code and/or source code for the Application, including any data
|
|
39
|
-
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
-
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
-
|
|
42
|
-
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
-
|
|
44
|
-
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
-
without being bound by section 3 of the GNU GPL.
|
|
46
|
-
|
|
47
|
-
2. Conveying Modified Versions.
|
|
48
|
-
|
|
49
|
-
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
-
facility refers to a function or data to be supplied by an Application
|
|
51
|
-
that uses the facility (other than as an argument passed when the
|
|
52
|
-
facility is invoked), then you may convey a copy of the modified
|
|
53
|
-
version:
|
|
54
|
-
|
|
55
|
-
a) under this License, provided that you make a good faith effort to
|
|
56
|
-
ensure that, in the event an Application does not supply the
|
|
57
|
-
function or data, the facility still operates, and performs
|
|
58
|
-
whatever part of its purpose remains meaningful, or
|
|
59
|
-
|
|
60
|
-
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
-
this License applicable to that copy.
|
|
62
|
-
|
|
63
|
-
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
-
|
|
65
|
-
The object code form of an Application may incorporate material from
|
|
66
|
-
a header file that is part of the Library. You may convey such object
|
|
67
|
-
code under terms of your choice, provided that, if the incorporated
|
|
68
|
-
material is not limited to numerical parameters, data structure
|
|
69
|
-
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
-
(ten or fewer lines in length), you do both of the following:
|
|
71
|
-
|
|
72
|
-
a) Give prominent notice with each copy of the object code that the
|
|
73
|
-
Library is used in it and that the Library and its use are
|
|
74
|
-
covered by this License.
|
|
75
|
-
|
|
76
|
-
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
-
document.
|
|
78
|
-
|
|
79
|
-
4. Combined Works.
|
|
80
|
-
|
|
81
|
-
You may convey a Combined Work under terms of your choice that,
|
|
82
|
-
taken together, effectively do not restrict modification of the
|
|
83
|
-
portions of the Library contained in the Combined Work and reverse
|
|
84
|
-
engineering for debugging such modifications, if you also do each of
|
|
85
|
-
the following:
|
|
86
|
-
|
|
87
|
-
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
-
the Library is used in it and that the Library and its use are
|
|
89
|
-
covered by this License.
|
|
90
|
-
|
|
91
|
-
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
-
document.
|
|
93
|
-
|
|
94
|
-
c) For a Combined Work that displays copyright notices during
|
|
95
|
-
execution, include the copyright notice for the Library among
|
|
96
|
-
these notices, as well as a reference directing the user to the
|
|
97
|
-
copies of the GNU GPL and this license document.
|
|
98
|
-
|
|
99
|
-
d) Do one of the following:
|
|
100
|
-
|
|
101
|
-
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
-
License, and the Corresponding Application Code in a form
|
|
103
|
-
suitable for, and under terms that permit, the user to
|
|
104
|
-
recombine or relink the Application with a modified version of
|
|
105
|
-
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
-
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
-
Corresponding Source.
|
|
108
|
-
|
|
109
|
-
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
-
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
-
a copy of the Library already present on the user's computer
|
|
112
|
-
system, and (b) will operate properly with a modified version
|
|
113
|
-
of the Library that is interface-compatible with the Linked
|
|
114
|
-
Version.
|
|
115
|
-
|
|
116
|
-
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
-
be required to provide such information under section 6 of the
|
|
118
|
-
GNU GPL, and only to the extent that such information is
|
|
119
|
-
necessary to install and execute a modified version of the
|
|
120
|
-
Combined Work produced by recombining or relinking the
|
|
121
|
-
Application with a modified version of the Linked Version. (If
|
|
122
|
-
you use option 4d0, the Installation Information must accompany
|
|
123
|
-
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
-
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
-
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
-
for conveying Corresponding Source.)
|
|
127
|
-
|
|
128
|
-
5. Combined Libraries.
|
|
129
|
-
|
|
130
|
-
You may place library facilities that are a work based on the
|
|
131
|
-
Library side by side in a single library together with other library
|
|
132
|
-
facilities that are not Applications and are not covered by this
|
|
133
|
-
License, and convey such a combined library under terms of your
|
|
134
|
-
choice, if you do both of the following:
|
|
135
|
-
|
|
136
|
-
a) Accompany the combined library with a copy of the same work based
|
|
137
|
-
on the Library, uncombined with any other library facilities,
|
|
138
|
-
conveyed under the terms of this License.
|
|
139
|
-
|
|
140
|
-
b) Give prominent notice with the combined library that part of it
|
|
141
|
-
is a work based on the Library, and explaining where to find the
|
|
142
|
-
accompanying uncombined form of the same work.
|
|
143
|
-
|
|
144
|
-
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
-
|
|
146
|
-
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
-
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
-
versions will be similar in spirit to the present version, but may
|
|
149
|
-
differ in detail to address new problems or concerns.
|
|
150
|
-
|
|
151
|
-
Each version is given a distinguishing version number. If the
|
|
152
|
-
Library as you received it specifies that a certain numbered version
|
|
153
|
-
of the GNU Lesser General Public License "or any later version"
|
|
154
|
-
applies to it, you have the option of following the terms and
|
|
155
|
-
conditions either of that published version or of any later version
|
|
156
|
-
published by the Free Software Foundation. If the Library as you
|
|
157
|
-
received it does not specify a version number of the GNU Lesser
|
|
158
|
-
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
-
General Public License ever published by the Free Software Foundation.
|
|
160
|
-
|
|
161
|
-
If the Library as you received it specifies that a proxy can decide
|
|
162
|
-
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
-
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
-
permanent authorization for you to choose that version for the
|
|
165
|
-
Library.
|
|
166
|
-
.
|
|
1
|
+
Copyright (c) 2014 Lyjia
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
8
|
+
list of conditions and the following disclaimer.
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
|
11
|
+
and/or other materials provided with the distribution.
|
|
12
|
+
|
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
14
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
15
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
16
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
17
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
18
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
19
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
20
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
21
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
22
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
23
|
+
|
|
24
|
+
The views and conclusions contained in the software and documentation are those
|
|
25
|
+
of the authors and should not be interpreted as representing official policies,
|
|
26
|
+
either expressed or implied, of the FreeBSD Project.
|
data/README.md
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
# SirCachealot
|
|
2
2
|

|
|
3
3
|
|
|
4
|
-
SirCachealot is
|
|
4
|
+
SirCachealot is an easy-to-use, in-memory, pluggable key-value store, available under the 2-clause BSD license. It is built for:
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
*
|
|
6
|
+
* Simple, swappable, modular backends. Cache server down? Swap another one in and keep chuggin'. Currently supports Redis and an in-memory store.
|
|
7
|
+
* Shared memory between processes. Multi-process environments (such as in Passenger) make shared state difficult.
|
|
8
|
+
* Unified API supporting a selected, shared subset of each backend's features.
|
|
9
|
+
* Easily and seamlessly deal with cache misses.
|
|
10
|
+
* Lets you cache stuff the Ruby way!
|
|
11
|
+
|
|
12
|
+
Here's a quick example, which caches a user object to avoid fetching it from the database:
|
|
13
|
+
|
|
14
|
+
def login(id, password_hash)
|
|
15
|
+
|
|
16
|
+
user = Sir.get(keyname) do |key| #Doesn't execute the block is key is found
|
|
17
|
+
Sir.put(key, User.find_by_id(id), 1.day) #Cache miss! So let's fetch the User and store it for a day
|
|
18
|
+
end (note: 1.day comes from Rails, not included)
|
|
19
|
+
|
|
20
|
+
# your code here
|
|
21
|
+
user.authenticate?(password_hash) #returns true if match, false is not
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Roadmap for 1.0:
|
|
26
|
+
* More storage backends: RailsCache, Postgres HSTORE, Mysql memtable, memcache, internal
|
|
27
|
+
* Add redis incr/decr and extra datatypes (emulate features on ramcache if necessary)
|
|
28
|
+
* Explore clustering applications
|
|
29
|
+
* Add convenience methods to ActiveRecord
|
|
8
30
|
|
|
9
31
|
## Installation
|
|
10
32
|
|
|
@@ -22,7 +44,9 @@ Or install it yourself as:
|
|
|
22
44
|
|
|
23
45
|
## Usage
|
|
24
46
|
|
|
25
|
-
SirCachealot exposes a new module named `Sir`. This module is designed to be available globally in both Rails apps and vanilla Ruby scripts (don't forget `require '
|
|
47
|
+
SirCachealot exposes a new module named `Sir`. This module is designed to be available globally in both Rails apps and vanilla Ruby scripts (don't forget `require 'sir'`!)
|
|
48
|
+
|
|
49
|
+
This section is a quick tour through some of SirCachealot's best features. For a comprehensive API reference, please refer to the comments in `Sir::Backends::Base`.
|
|
26
50
|
|
|
27
51
|
**You can use SirCachealot immediately, using either:**
|
|
28
52
|
|
|
@@ -35,44 +59,116 @@ If `config(:delete_on_nil) == true` and `value == nil`, `put()` will return `tru
|
|
|
35
59
|
|
|
36
60
|
**You can retreive the value later, if it hasn't expired, with:**
|
|
37
61
|
|
|
38
|
-
my_var = Sir.get(keyname)
|
|
39
|
-
my_var = Sir.get(keyname, true) # for a deep copy (crude way, Marshal.load(Marshal.dump(obj)) )
|
|
62
|
+
my_var = Sir.get(keyname)
|
|
40
63
|
|
|
41
64
|
or
|
|
42
65
|
|
|
43
66
|
# A convenient way to rectify a cache miss!
|
|
44
67
|
# put() returns the object you give it
|
|
45
|
-
my_var = Sir.get(keyname) do
|
|
46
|
-
Sir.put(
|
|
68
|
+
my_var = Sir.get(keyname) do |key|
|
|
69
|
+
Sir.put(key, User.find_by_id(id))
|
|
47
70
|
end
|
|
48
71
|
|
|
49
72
|
If the key does not exist, or if it has expired:
|
|
73
|
+
|
|
50
74
|
* `get()` returns `nil` if not given a block to execute.
|
|
51
75
|
* `get()` yields to code block, if one is supplied.
|
|
52
76
|
|
|
53
77
|
**To delete a cache entry, you can:**
|
|
54
78
|
|
|
55
|
-
Sir.
|
|
79
|
+
Sir.kill(key)
|
|
56
80
|
|
|
57
81
|
**If you want to clear the cache, you can:**
|
|
58
82
|
|
|
59
|
-
Sir.
|
|
83
|
+
Sir.nuke
|
|
60
84
|
|
|
61
85
|
**If you want to sweep and purge all expired entries, you can:**
|
|
62
86
|
|
|
63
|
-
Sir.
|
|
87
|
+
Sir.sweep
|
|
64
88
|
|
|
65
89
|
**There are a few configuration options available. You can configure SirCachealot with:**
|
|
66
90
|
|
|
91
|
+
cache_opts = Sir::Backends::RamCache::DEFAULTS #Change RamCache to RedisCache for redis
|
|
92
|
+
|
|
67
93
|
Sir.configure do |config|
|
|
68
94
|
config[:default_expiry] = 3600 # default expiration timeout in seconds
|
|
69
|
-
config[:mode] = :ram_cache # cache storage mode. Currently
|
|
95
|
+
config[:mode] = :ram_cache # cache storage mode. Currently: :ram_cache, :redis_cache
|
|
70
96
|
config[:debug] = true|false # show some debug messages
|
|
71
97
|
config[:annoy] = true|false # show even more debug messages
|
|
72
|
-
|
|
98
|
+
config[:options] = cache_opts # optional, depending on backend
|
|
73
99
|
end
|
|
74
100
|
|
|
75
|
-
|
|
101
|
+
Note: Backends may have additional configuration parameters that need to be satisfied. The default configuration can be retreived from the `DEFAULTS` constant in the backend class, as shown above. These values may then be modified and passed back to `Sir.configure()`
|
|
102
|
+
|
|
103
|
+
## API Reference
|
|
104
|
+
|
|
105
|
+
See `Sir::Backends::Base`
|
|
106
|
+
|
|
107
|
+
## Available Backends
|
|
108
|
+
|
|
109
|
+
### RAM Cache (for testing)
|
|
110
|
+
The RAM Cache stores cache entries in a Ruby hash, and is the default cache store that SirCachealot will use if left unconfigured.
|
|
111
|
+
|
|
112
|
+
RAM Cache does not support automatic expiration, and so it must be periodically #swept(). RAM Cache's thread safety depends on your interpreter: 'green thread' implementations are safe, while true multi-threaded environments (such as JRuby) remain untested at this time.
|
|
113
|
+
|
|
114
|
+
This backend module is intentionally left simple, and is of limited usefulness. It is designed to be an example for implementing additional backends, and to satisfy basic turn-key functionality.
|
|
115
|
+
|
|
116
|
+
#### Configuration
|
|
117
|
+
Note that #configure is not necessary if you wish to use RAM cache with its default settings.
|
|
118
|
+
|
|
119
|
+
Sir.configure do |config|
|
|
120
|
+
config[:default_expiry] = 3600
|
|
121
|
+
config[:mode] = :ram_cache
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
### Redis Cache
|
|
125
|
+
Redis cache supports a subset of full Redis functionality.
|
|
126
|
+
|
|
127
|
+
#### Configuration
|
|
128
|
+
|
|
129
|
+
redis_obj = Redis.new(:path => "/tmp/redis.sock")
|
|
130
|
+
|
|
131
|
+
cache_opts = Sir::Backends::RedisCache::DEFAULTS
|
|
132
|
+
cache_opts[:redis_obj] = redis_obj #supply a preconfigured Redis instance
|
|
133
|
+
cache_opts[:namespace] = "MyCacheStore" #override the default "SirCachealot" namespace
|
|
134
|
+
cache_opts[:serializer] = :marshal #all requests to put() are serialized. :marshal or :json are available
|
|
135
|
+
|
|
136
|
+
Sir.configure do |config|
|
|
137
|
+
config[:default_expiry] = 3600
|
|
138
|
+
config[:mode] = :redis_cache
|
|
139
|
+
config[:options] = cache_opts
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
##### Options
|
|
143
|
+
|
|
144
|
+
* `:redis_obj`: Store your pre-configured `Redis` instance in here
|
|
145
|
+
* `:namespace`: Internally, all keys are prefixed with "SirCachealot" while stored in Redis. You can change that string with this option.
|
|
146
|
+
* `:serializer`: `:marhsal` to serialize all values with `Marshal.load()`, or :json to serialize all values with `.to_json`
|
|
147
|
+
* Note: Do not switch to a different serializer without first calling `Sir.nuke()`!
|
|
148
|
+
|
|
149
|
+
## Testing
|
|
150
|
+
A test suite is available through `rspec`.
|
|
151
|
+
|
|
152
|
+
sir_cachealot 0.6.0 was tested against ruby 1.9.3, 2.0, and 2.1 and passed 27/27 tests.
|
|
153
|
+
|
|
154
|
+
### Testing SirCachealot
|
|
155
|
+
|
|
156
|
+
$> cd $GEM_PATH # cd into your gem path. The shown command, as written, will fail if you use rvm.
|
|
157
|
+
$> cd gems/sir_cachealot-0.6.0
|
|
158
|
+
$> bundle install
|
|
159
|
+
$> rspec
|
|
160
|
+
|
|
161
|
+
## Change Log
|
|
162
|
+
|
|
163
|
+
### 0.6.0
|
|
164
|
+
|
|
165
|
+
* Modularized backends.
|
|
166
|
+
* Implemented Redis Cache features: FLUSHDB (`nuke()`, KEYS (`keys()`), DBSIZE (`length()`), BGSAVE (`flush()`)
|
|
167
|
+
* Renamed clean, clear functions to sweep, nuke (to be easier to visually differentiate)
|
|
168
|
+
|
|
169
|
+
## Known issues
|
|
170
|
+
|
|
171
|
+
* `Marshal.load()`, which is used by RedisCache's `:marshal` serializer, has been known to throw exceptions when it is called repeatedly (like in a tight loop)
|
|
76
172
|
|
|
77
173
|
## Contributing
|
|
78
174
|
|
|
@@ -84,5 +180,5 @@ If the key does not exist, or if it has expired:
|
|
|
84
180
|
|
|
85
181
|
## License
|
|
86
182
|
|
|
87
|
-
Use of SirCachealot is permitted under the terms of [
|
|
88
|
-
For more information see LICENSE.txt.
|
|
183
|
+
Use of SirCachealot is permitted under the terms of the [2-clause BSD License](http://directory.fsf.org/wiki?title=License:FreeBSD).
|
|
184
|
+
For more information see LICENSE.txt.
|
data/Rakefile
CHANGED
data/lib/sir.rb
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require "rubygems"
|
|
2
|
+
|
|
3
|
+
def required(str)
|
|
4
|
+
require File.dirname(__FILE__) + "/" + str
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
required "sir/version"
|
|
8
|
+
required "sir/backends"
|
|
9
|
+
|
|
10
|
+
module Sir
|
|
11
|
+
|
|
12
|
+
BACKENDS = {
|
|
13
|
+
ram_cache: Sir::Backends::RamCache,
|
|
14
|
+
redis_cache: Sir::Backends::RedisCache
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@@backend = Sir::Backends::RamCache
|
|
18
|
+
|
|
19
|
+
@@configuration = {
|
|
20
|
+
mode: :ram_cache,
|
|
21
|
+
debug: false, #debug messages (prints to stderr)
|
|
22
|
+
annoy: false, #super annoying debug messages (prints to stderr)
|
|
23
|
+
default_expiry: 3600, #Integer(1.hour)
|
|
24
|
+
options: {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Configuration function
|
|
29
|
+
# yields a config block, see README
|
|
30
|
+
# @returns true
|
|
31
|
+
def self.configure(&block)
|
|
32
|
+
|
|
33
|
+
if block_given?
|
|
34
|
+
yield(@@configuration)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
#ap @@configuration
|
|
38
|
+
|
|
39
|
+
@@backend = BACKENDS[@@configuration[:mode]]
|
|
40
|
+
@@backend.configure(@@configuration[:options])
|
|
41
|
+
|
|
42
|
+
# this doesnt work right
|
|
43
|
+
#Sir::Backends::Base::EXPORTS.each do |func|
|
|
44
|
+
# Sir.send :define_singleton_method, func do |*params|
|
|
45
|
+
# @@backend.send(func, *params)
|
|
46
|
+
# end
|
|
47
|
+
# self.annoy("Attached #{func}")
|
|
48
|
+
#end
|
|
49
|
+
|
|
50
|
+
self.debug("SirCachealot #{Sir::VERSION} loaded configuration for #{@@configuration[:mode]}, watching #{Sir::Backends::Base::EXPORTS.length} methods")
|
|
51
|
+
self.annoy("Annoy activated! Bwahaha!")
|
|
52
|
+
return true
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# Tests is debug flag is set
|
|
58
|
+
# @returns [boolean] debug status
|
|
59
|
+
def self.debug?
|
|
60
|
+
return @@configuration[:debug]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# Tests if annoy flag is set
|
|
65
|
+
# @returns [boolean] annoy status
|
|
66
|
+
def self.annoy?
|
|
67
|
+
return @@configuration[:annoy]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
# Send message to debug stream
|
|
72
|
+
def self.debug(msg)
|
|
73
|
+
$stderr.puts("<=S=I=R=[==] !! #{msg}") if self.debug?
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Send message to annoy stream
|
|
78
|
+
def self.annoy(msg)
|
|
79
|
+
$stderr.puts("<=S=I=R=[==] - #{msg}") if self.annoy?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# look up value of single configuration option
|
|
84
|
+
def self.config(key)
|
|
85
|
+
return @@configuration[key]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def self.dump_config
|
|
90
|
+
p @@configuration
|
|
91
|
+
return nil
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# TODO: define all these methods on configure(), we should only go here if the user hasnt configured Sir
|
|
96
|
+
# catch on use if unconfigured
|
|
97
|
+
def self.method_missing(meth, *args, &block)
|
|
98
|
+
|
|
99
|
+
if Sir::Backends::Base::EXPORTS.include?(meth)
|
|
100
|
+
self.configure! if @@configuration.nil?
|
|
101
|
+
return @@backend.send(meth, *args, &block)
|
|
102
|
+
else
|
|
103
|
+
super
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
#remove me
|
|
109
|
+
#
|
|
110
|
+
# @todo Remove me
|
|
111
|
+
def self.conredis
|
|
112
|
+
require 'redis'
|
|
113
|
+
redis_obj = Redis.new(:host => "127.0.0.1", :port => "6379")
|
|
114
|
+
opts = Sir::Backends::RedisCache::DEFAULTS
|
|
115
|
+
opts[:redis_obj] = redis_obj
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
Sir.configure do |config|
|
|
119
|
+
config[:mode] = :redis_cache
|
|
120
|
+
config[:debug] = true
|
|
121
|
+
config[:options] = opts
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
def self.puke
|
|
130
|
+
raise TypeError, "Invalid config(:mode). Check the inputs sent to Sir.configure()"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def self.crude_clone(obj)
|
|
135
|
+
return Marshal.load(Marshal.dump(obj))
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# returns a namespaced key
|
|
140
|
+
def self.nsed_key(key)
|
|
141
|
+
return "SirCachealot-#{key}"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
end
|