sir_cachealot 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .idea
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sir_cachealot.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
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
+ .
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # SirCachealot
2
+ ![Sir Cachealot graphic](https://github.com/lyjia/sircachealot/blob/master/sircachealot.png?raw=true "Sir Cachealot graphic")
3
+
4
+ SirCachealot is a very simple memcache-like keystore, licensed under LGPLv3. It is built to be modular and present a consistent API.
5
+
6
+ Future plans for 1.0:
7
+ * Rails cache, Redis, and memcache storage modes.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'SirCachealot'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install SirCachealot
22
+
23
+ ## Usage
24
+
25
+ SirCachealot creates a new globally-accessible class named `Sir`.
26
+
27
+ **You can use SirCachealot immediately, using either:**
28
+
29
+ Sir.put(keyname, value)
30
+ Sir.put(keyname, value, expiry) # expiry can be relative or absolute time expressed in seconds (Fixnum) or a Time object.
31
+
32
+ `put()` will return the object you gave it. This is useful if you wish to use `get()`'s `yield` functionality.
33
+
34
+ **You can retreive the value later, if it hasn't expired, with:**
35
+
36
+ my_var = Sir.get(keyname)
37
+
38
+ or
39
+
40
+ # put() returns the object you give it
41
+ my_var = Sir.get(keyname) do
42
+ Sir.put(keyname, User.find_by_id(id))
43
+ end
44
+
45
+ If the key does not exist, or if it has expired:
46
+ * `get()` returns `nil` if not given a block to execute.
47
+ * `get()` yields to code block, if one is supplied.
48
+
49
+ **If you want to clear the cache, you can:**
50
+
51
+ Sir.clear
52
+
53
+ **If you want to sweep and purge all expired entries, you can:**
54
+
55
+ Sir.clean
56
+
57
+ **There are a few configuration options available. You can configure SirCachealot with:**
58
+
59
+ Sir.configure do |config|
60
+ config[:default_expiry] = 3600 # default expiration timeout in seconds
61
+ config[:mode] = :ram_cache # cache storage mode. Currently only :ram_cache is supported. Others may be added at a later date.
62
+ config[:debug] = true|false # show some debug messages
63
+ config[:annoy] = true|false # show even more debug messages
64
+ end
65
+
66
+ *Note: Config keynames are always `.downcase.to_sym`!*
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
75
+
76
+ ## License
77
+
78
+ Use of SirCachealot is permitted under the terms of [LGPLv3](http://www.gnu.org/licenses/lgpl-3.0.txt).
79
+ For more information see LICENSE.txt.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => [:spec]
7
+
8
+ desc "Open an irb session preloaded with this library"
9
+ task :console do
10
+ sh "irb -rubygems -I lib -r sir_cachealot.rb"
11
+ end
@@ -0,0 +1,3 @@
1
+ module SirCachealot
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,194 @@
1
+ require "sir_cachealot/version"
2
+
3
+ module Sir
4
+
5
+ RAM = :ram_cache
6
+
7
+ @@ram_cache = { }
8
+
9
+ @@configuration = {
10
+ mode: RAM,
11
+ debug: false,
12
+ annoy: false, #super annoying debug messages
13
+ default_expiry: 3600, #Integer(1.hour),
14
+
15
+ }
16
+
17
+ #Configuration parameters
18
+ # configure() yields a block, as per convention
19
+ # configure(key) returns the value of key, or nil
20
+ # configure(key, value) sets key to value
21
+ def self.configure(key = nil, value = nil)
22
+
23
+ key = key.downcase.to_sym unless key.nil?
24
+
25
+ if !key.nil? && !value.nil?
26
+ @@configuration[key] = value
27
+ return value
28
+
29
+ elsif !key.nil? && value.nil?
30
+ return @@configuration[key]
31
+
32
+ elsif key.nil? && value.nil?
33
+ yield(@@configuration)
34
+
35
+ #normalize keynames
36
+ @@configuration.keys.each do |k|
37
+
38
+ knew = k.downcase.to_sym
39
+
40
+ unless knew == k
41
+ @@configuration[knew] = @@configuration[k]
42
+ @@configuration.delete(k)
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ class << self
52
+ alias :config :configure
53
+ end
54
+
55
+
56
+ # Gets value for supplied key
57
+ # Returns nil if empty or expired
58
+ def self.get(key)
59
+
60
+ case config(:mode)
61
+ when RAM
62
+
63
+ if x = @@ram_cache[key]
64
+
65
+ if x[:expiry] > Time.now
66
+ return x[:value]
67
+ else
68
+ # cache entry is stale
69
+ puts (" SirCachealot: Cache entry <#{key}> expired at #{x[:expiry]}. Deleting...") if config(:debug)
70
+ @@ram_cache.delete(key)
71
+ if block_given?
72
+ yield
73
+ else
74
+ nil
75
+ end
76
+ end
77
+
78
+ else
79
+
80
+ if block_given?
81
+ yield
82
+ else
83
+ return nil
84
+ end
85
+ end
86
+
87
+ else
88
+ puke
89
+ end
90
+
91
+ end
92
+
93
+ # Puts value in cache, key is param key
94
+ # Param expiry is optional, it can be a relative or absolute Fixnum or Time object
95
+ # Returns expiry time in absolute form (e.g. 3:56pm instead of 3 hours from now)
96
+ def self.put(key, value, expiry = config(:default_expiry))
97
+
98
+ case expiry.class.name
99
+ when "Fixnum"
100
+ true
101
+ when "Time"
102
+ expiry = Integer(expiry)
103
+ else
104
+ raise ArgumentError, "Expiry must be a Fixnum or Time object"
105
+ end
106
+
107
+
108
+ unless (expiry > Integer(Time.now))
109
+ expiry = Time.now + expiry
110
+ end
111
+
112
+ puts " SirCachealot: Will expire <#{key}> at #{expiry}" if config(:annoy) == true
113
+
114
+ case config(:mode)
115
+ when RAM
116
+
117
+ @@ram_cache[key] ||= { }
118
+ @@ram_cache[key][:value] = value
119
+ @@ram_cache[key][:expiry] = expiry
120
+ return value
121
+
122
+ else
123
+ puke
124
+ end
125
+
126
+
127
+ end
128
+
129
+ # Gets the qty of keys in the cache
130
+ def self.size?
131
+
132
+ case config(:mode)
133
+ when RAM
134
+ return @@ram_cache.count
135
+ else
136
+ puke
137
+ end
138
+
139
+ end
140
+
141
+ # Dumps list of cache keys to console, with their value's class.name and expiry
142
+ def self.dump
143
+
144
+ case config(:mode)
145
+ when RAM
146
+ @@ram_cache.each do |k, v|
147
+ puts("%-20s %-20s %20s" % [k, v[:value].class, v[:expiry]])
148
+ end
149
+ else
150
+ puke
151
+ end
152
+
153
+
154
+ return nil
155
+
156
+ end
157
+
158
+ # Clears the cache, erases all cache entries
159
+ def self.clear
160
+
161
+ case config(:mode)
162
+ when RAM
163
+ @@ram_cache = { }
164
+ else
165
+ puke
166
+ end
167
+
168
+ end
169
+
170
+ # Sweeps the cache for expired keys and purges them. NOT THREAD-SAFE!
171
+ def self.clean
172
+
173
+ case config(:mode)
174
+
175
+ when RAM
176
+ @@ram_cache.each_key do |k|
177
+ if @@ram_cache[k][:expiry] < Time.now
178
+ puts(" SirCachealot: Cleaned #{k}") if config(:debug)
179
+ @@ram_cache.delete(k)
180
+ end
181
+ end
182
+ else
183
+ puke
184
+ end
185
+
186
+ end
187
+
188
+ private
189
+
190
+ def self.puke
191
+ raise TypeError, "Invalid config(:mode). Check the inputs sent to Sir.configure()"
192
+ end
193
+
194
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sir_cachealot/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sir_cachealot"
8
+ gem.version = SirCachealot::VERSION
9
+ gem.authors = ["Lyjia"]
10
+ gem.email = ["tom@tomcorelis.com"]
11
+ gem.description = %q{A dead simple RAM keystore}
12
+ gem.summary = %q{SirCachealot is a drop-in memcache-like RAM cache for Ruby. Cache entries are saved and recalled by a key string, and their values can be whatever a Ruby hash can hold. Values can also expire, however expiration is only checked when the key is called, or a manual sweeper is run. }
13
+ gem.homepage = "http://www.lyjia.us"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec', '~> 2.5'
21
+
22
+ end
data/sircachealot.png ADDED
Binary file
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ TEST = "test"
4
+ DEFAULT_EXPIRY = 600
5
+
6
+ describe SirCachealot do
7
+
8
+ it 'should be configurable' do
9
+
10
+ Sir.configure do |config|
11
+ config[:default_expiry] = DEFAULT_EXPIRY
12
+ config["MODE"] = :ram_cache
13
+ config[:debug] = true
14
+ end
15
+
16
+ (Sir.config(:default_expiry) == DEFAULT_EXPIRY && Sir.config("MODE") == :ram_cache).should == true
17
+
18
+ end
19
+
20
+ it 'should return the key that is put into it' do
21
+ Sir.put(:test, TEST).should == TEST
22
+ end
23
+
24
+ it 'should return an arbitrary key' do
25
+
26
+ (Sir.get(:test) == TEST).should == true
27
+
28
+ end
29
+
30
+ it 'should yield when given a block and a key that does not exist' do
31
+ (Sir.get(:asdoiajdoaijdaodijaodiajdoaidjaodijaodij) { 5 }).should == 5
32
+ end
33
+
34
+ it 'should return nil when not given a block and a key that does not exist' do
35
+ (Sir.get(:asdoiajdoaijdaodijaodiajdoaidjaodijaodij)).should == nil
36
+ end
37
+
38
+ it 'should immediately expire a 0-expiry key' do
39
+ Sir.put(:expire, TEST, 0)
40
+ sleep(1)
41
+
42
+ Sir.get(:expire).should == nil
43
+ end
44
+
45
+ it 'should report its size correctly' do
46
+
47
+ Sir.size?.should == 1
48
+
49
+ end
50
+
51
+ it 'should clean() correctly' do
52
+
53
+ Sir.clear
54
+
55
+ Sir.put(:clean, TEST, 0)
56
+ Sir.clean
57
+
58
+ Sir.size?.should == 0
59
+
60
+
61
+ end
62
+
63
+ it 'should dump() correctly' do
64
+ Sir.put(:dump, TEST)
65
+ Sir.dump
66
+ end
67
+
68
+
69
+
70
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'sir_cachealot'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.formatter = 'documentation'
7
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sir_cachealot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lyjia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.5'
30
+ description: A dead simple RAM keystore
31
+ email:
32
+ - tom@tomcorelis.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/sir_cachealot.rb
43
+ - lib/sir_cachealot/version.rb
44
+ - sir_cachealot.gemspec
45
+ - sircachealot.png
46
+ - spec/sir_cachealot_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: http://www.lyjia.us
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: SirCachealot is a drop-in memcache-like RAM cache for Ruby. Cache entries
72
+ are saved and recalled by a key string, and their values can be whatever a Ruby
73
+ hash can hold. Values can also expire, however expiration is only checked when the
74
+ key is called, or a manual sweeper is run.
75
+ test_files:
76
+ - spec/sir_cachealot_spec.rb
77
+ - spec/spec_helper.rb