mini_memory_store 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ gem 'rake'
5
+ gem 'rspec', '~>2'
6
+ gem 'jeweler'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rspec (2.3.0)
12
+ rspec-core (~> 2.3.0)
13
+ rspec-expectations (~> 2.3.0)
14
+ rspec-mocks (~> 2.3.0)
15
+ rspec-core (2.3.1)
16
+ rspec-expectations (2.3.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.3.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ jeweler
25
+ rake
26
+ rspec (~> 2)
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ task :default => :spec
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new(:spec) do |t|
4
+ t.rspec_opts = '--backtrace --color'
5
+ end
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = 'mini_memory_store'
11
+ gem.summary = "Store a single value in memory with expiration"
12
+ gem.email = "michael@grosser.it"
13
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
14
+ gem.authors = ["Michael Grosser"]
15
+ end
16
+
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
20
+ end
data/Readme.md ADDED
@@ -0,0 +1,15 @@
1
+ Store a single value in memory with expiration
2
+
3
+ Install
4
+ =======
5
+ sudo gem install mini_memory_store
6
+
7
+ Usage
8
+ =====
9
+ CODE_EXAMPLE
10
+
11
+ Author
12
+ ======
13
+ [Michael Grosser](http://grosser.it)
14
+ grosser.michael@gmail.com
15
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,43 @@
1
+ class MiniMemoryStore
2
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
3
+
4
+ def initialize(options={})
5
+ @options = options
6
+ @last_set = 0
7
+ end
8
+
9
+ def set(value)
10
+ @last_set = Time.now.to_i
11
+ @value = value
12
+ end
13
+
14
+ def get
15
+ if expired?
16
+ nil
17
+ else
18
+ @value
19
+ end
20
+ end
21
+
22
+ def cache(&block)
23
+ value = get
24
+ if value.nil?
25
+ value = block.call
26
+ set(value)
27
+ value
28
+ else
29
+ value
30
+ end
31
+ end
32
+
33
+ def clear
34
+ set nil
35
+ end
36
+
37
+ private
38
+
39
+ def expired?
40
+ return unless @options[:expires_in]
41
+ (Time.now.to_i - @options[:expires_in]) >= @last_set
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mini_memory_store}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2010-12-22}
13
+ s.email = %q{michael@grosser.it}
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "lib/mini_memory_store.rb",
21
+ "mini_memory_store.gemspec",
22
+ "spec/mini_memory_store_spec.rb",
23
+ "spec/spec_helper.rb"
24
+ ]
25
+ s.homepage = %q{http://github.com/grosser/mini_memory_store}
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.3.7}
28
+ s.summary = %q{Store a single value in memory with expiration}
29
+ s.test_files = [
30
+ "spec/mini_memory_store_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
+ s.add_development_dependency(%q<rake>, [">= 0"])
40
+ s.add_development_dependency(%q<rspec>, ["~> 2"])
41
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
42
+ else
43
+ s.add_dependency(%q<rake>, [">= 0"])
44
+ s.add_dependency(%q<rspec>, ["~> 2"])
45
+ s.add_dependency(%q<jeweler>, [">= 0"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<rake>, [">= 0"])
49
+ s.add_dependency(%q<rspec>, ["~> 2"])
50
+ s.add_dependency(%q<jeweler>, [">= 0"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,59 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe MiniMemoryStore do
4
+ let(:store){ MiniMemoryStore.new }
5
+
6
+ it "has a VERSION" do
7
+ MiniMemoryStore::VERSION.should =~ /^\d+\.\d+\.\d+$/
8
+ end
9
+
10
+ it "is nil when not set" do
11
+ store.get.should == nil
12
+ end
13
+
14
+ it "can store a value" do
15
+ store.set 1
16
+ store.get.should == 1
17
+ end
18
+
19
+ it "can cache a value" do
20
+ store.cache{1}.should == 1
21
+ store.cache{2}.should == 1
22
+ end
23
+
24
+ it "can cache false" do
25
+ store.cache{false}.should == false
26
+ store.cache{1}.should == false
27
+ end
28
+
29
+ it "does not cache nil" do
30
+ store.cache{nil}.should == nil
31
+ store.cache{1}.should == 1
32
+ end
33
+
34
+ it "clears" do
35
+ store.set 1
36
+ store.clear
37
+ store.get.should == nil
38
+ end
39
+
40
+ it "does not expire" do
41
+ store.set 1
42
+ sleep 1
43
+ store.get.should == 1
44
+ end
45
+
46
+ it "expires after given time" do
47
+ store = MiniMemoryStore.new(:expires_in => 1)
48
+ store.set 1
49
+ store.get.should == 1
50
+ sleep 1
51
+ store.get.should == nil
52
+ end
53
+
54
+ it "expires immediately with expires_in => 0" do
55
+ store = MiniMemoryStore.new(:expires_in => 0)
56
+ store.set 1
57
+ store.get.should == nil
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'mini_memory_store'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_memory_store
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Grosser
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-22 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ name: rake
32
+ prerelease: false
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 7
42
+ segments:
43
+ - 2
44
+ version: "2"
45
+ name: rspec
46
+ prerelease: false
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ name: jeweler
60
+ prerelease: false
61
+ type: :development
62
+ version_requirements: *id003
63
+ description:
64
+ email: michael@grosser.it
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - Rakefile
75
+ - Readme.md
76
+ - VERSION
77
+ - lib/mini_memory_store.rb
78
+ - mini_memory_store.gemspec
79
+ - spec/mini_memory_store_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/grosser/mini_memory_store
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project:
111
+ rubygems_version: 1.3.7
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Store a single value in memory with expiration
115
+ test_files:
116
+ - spec/mini_memory_store_spec.rb
117
+ - spec/spec_helper.rb