local_cache 1.0.0 → 1.2.0

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.
@@ -0,0 +1,26 @@
1
+ LocalCache
2
+ ==========
3
+
4
+ A local in memory cache, similar to Rails' built in MemoryStore, but adds more advanced features
5
+ like cache size limit and object expiration / ttl.
6
+
7
+ Usage
8
+ ------
9
+
10
+ ### For Rails
11
+
12
+ In environment.rb:
13
+
14
+ require 'local_cache'
15
+
16
+ config.cache_store = LocalCache.new
17
+
18
+ See rdoc for initialization options such as max size and default ttl.
19
+
20
+ ### For regular usage
21
+
22
+ cache = LocalCache.new
23
+ cache.write("key", value)
24
+ val = cache.read("key")
25
+
26
+
@@ -1,71 +1,114 @@
1
- require 'active_support'
2
-
3
- module ActiveSupport
4
- module Cache
5
- class LocalCache < Store
6
- VERSION = '1.0.0'
7
-
8
- def initialize(size=1000)
9
- puts 'Creating new LocalCache'
10
- @size = size
11
- # we initialize an empty hash
12
- @cache = {}
13
- @cache_list = []
14
- end
15
-
16
- def get(key)
17
- # if the API URL exists as a key in cache, we just return it
18
- # we also make sure the data is fresh
19
- if @cache.has_key? key
20
- expires = @cache[key][0]
21
- if expires - Time.now > 0
22
- # puts 'returning from cache ' + @cache[key][1].inspect
23
- return @cache[key][1]
24
- else
25
- # puts 'expired=' + key + ' at ' + expires.to_s + ' and now=' + Time.now.to_s
26
- delete(key)
27
- end
28
- else
29
- # puts 'cache does not contain ' + key
30
- end
31
- return nil
32
- end
33
-
34
- def put(key, val, seconds_to_store)
35
- seconds_to_store = seconds_to_store || 9999999
36
- # puts 'seconds=' + seconds_to_store.to_s
37
- @cache[key] = [Time.now+seconds_to_store, val]
38
- @cache_list << key
39
- while @cache.size > @size && @cache_list.size > 0
40
- to_remove = @cache_list.pop
41
- @cache.delete(to_remove) unless to_remove.nil?
42
- end
43
- end
44
-
45
- def read(name, options = nil)
46
- # puts 'read from localcache'
47
- super
48
- ret = get(name)
49
- # puts 'ret.frozen=' + ret.frozen?.to_s
50
- return ret
51
- end
52
-
53
- def write(name, value, options = nil)
54
- super
55
- put(name, value, options.nil? ? nil : options[:expires_in])
56
- # puts 'write.frozen=' + value.frozen?.to_s
57
- end
58
-
59
- def delete(name, options = nil)
60
- super
61
- @cache.delete(name)
62
- end
63
-
64
- def delete_matched(matcher, options = nil)
65
- super
66
- raise "delete_matched not supported by LocalCache"
67
- end
68
-
69
- end
70
- end
71
- end
1
+ require 'active_support'
2
+
3
+
4
+ class LocalCache < ActiveSupport::Cache::Store
5
+
6
+ # Initialize a new LocalCache.
7
+ #
8
+ # Options:
9
+ # :size => 1000 - max number of objects to store
10
+ # :ttl => 60 - default ttl (can be overridden on any PUT/WRITE)
11
+ def initialize(options={})
12
+ puts 'Creating new LocalCache'
13
+ @size = options[:size] || 1000
14
+ @default_expires_in = options[:ttl] || 60
15
+ # we initialize an empty hash
16
+ @cache = {}
17
+ # and a list for pushing objects out
18
+ @cache_list = []
19
+ end
20
+
21
+ def get(key)
22
+ # if the API URL exists as a key in cache, we just return it
23
+ # we also make sure the data is fresh
24
+ #puts 'looking in cache for: ' + key.to_s
25
+ if @cache.has_key? key
26
+ expires = @cache[key][0]
27
+ #puts 'checking expired=' + key + ' at ' + expires.to_s + ' and now=' + Time.now.to_s
28
+ if expires - Time.now > 0
29
+ # puts 'returning from cache ' + @cache[key][1].inspect
30
+ # todo: implement LRU ordering
31
+ return @cache[key][1]
32
+ else
33
+ #puts 'expired'
34
+ delete(key)
35
+ end
36
+ else
37
+ # puts 'cache does not contain ' + key
38
+ end
39
+ return nil
40
+ end
41
+
42
+ def get_multi(keys, raw=false)
43
+ ret = {}
44
+ keys.each do |k|
45
+ val = get(k)
46
+ ret[k] = val unless val.nil?
47
+ end
48
+ ret
49
+ end
50
+
51
+ def get_i(key)
52
+ val = get(key)
53
+ return nil if val.nil?
54
+ return val.to_i
55
+ end
56
+
57
+ def exist?(key)
58
+ return !get(key).nil?
59
+ end
60
+
61
+ def increment(key, val=1)
62
+ ret = get(key)
63
+ if ret.is_a?(Fixnum)
64
+ ret += val
65
+ else
66
+ ret = val
67
+ put(key, ret)
68
+ end
69
+ end
70
+
71
+ def put(key, val, seconds_to_store=999999, raw=false)
72
+ seconds_to_store = seconds_to_store || @default_expires_in
73
+ #puts 'seconds=' + seconds_to_store.to_s
74
+ @cache[key] = [Time.now+seconds_to_store, val]
75
+ @cache_list << key
76
+ while @cache.size > @size && @cache_list.size > 0
77
+ to_remove = @cache_list.pop
78
+ @cache.delete(to_remove) unless to_remove.nil?
79
+ end
80
+ end
81
+
82
+ def read(name, options = nil)
83
+ # puts 'read from localcache'
84
+ super
85
+ ret = get(name)
86
+ # puts 'ret.frozen=' + ret.frozen?.to_s
87
+ return ret
88
+ end
89
+
90
+ def write(name, value, options = nil)
91
+ super
92
+ put(name, value, options.nil? ? nil : options[:expires_in])
93
+ # puts 'write.frozen=' + value.frozen?.to_s
94
+ end
95
+
96
+ def delete(name, options = nil)
97
+ super
98
+ @cache.delete(name)
99
+ end
100
+
101
+ def delete_matched(matcher, options = nil)
102
+ super
103
+ raise "delete_matched not supported by LocalCache"
104
+ end
105
+
106
+ end
107
+
108
+ # Backwards compatibility
109
+ module ActiveSupport
110
+ module Cache
111
+ class LocalCache < ::LocalCache
112
+ end
113
+ end
114
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: local_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -9,53 +9,28 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-23 00:00:00 -07:00
12
+ date: 2009-10-21 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.12.2
24
- version:
25
- description: |-
26
- A local in memory cache with max size and expiration.
27
-
28
- Can be used as a Rails cache too. In environment.rb:
29
-
30
- require 'local_cache'
31
- ActionController::Base.cache_store = :local_cache
32
- Activerecordtosdb.cache_store = ActionController::Base.cache_store
33
- email:
34
- - travis@crankapps.com
35
- executables:
36
- - local_cache
14
+ dependencies: []
15
+
16
+ description: Similar to Rails' built in MemoryStore, but adds size limit and expiration.
17
+ email: travis@appoxy.com
18
+ executables: []
19
+
37
20
  extensions: []
38
21
 
39
22
  extra_rdoc_files:
40
- - History.txt
41
- - Manifest.txt
42
- - README.txt
23
+ - README.markdown
43
24
  files:
44
- - History.txt
45
- - Manifest.txt
46
- - README.txt
47
- - Rakefile
48
- - bin/local_cache
49
25
  - lib/local_cache.rb
50
- - test/test_local_cache.rb
26
+ - README.markdown
51
27
  has_rdoc: true
52
- homepage: http://www.crankapps.com
28
+ homepage: http://github.com/appoxy/local_cache/
53
29
  licenses: []
54
30
 
55
31
  post_install_message:
56
32
  rdoc_options:
57
- - --main
58
- - README.txt
33
+ - --charset=UTF-8
59
34
  require_paths:
60
35
  - lib
61
36
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -72,10 +47,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
47
  version:
73
48
  requirements: []
74
49
 
75
- rubyforge_project: spacegems
76
- rubygems_version: 1.3.2
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.5
77
52
  signing_key:
78
53
  specification_version: 3
79
- summary: A local in memory cache with max size and expiration
80
- test_files:
81
- - test/test_local_cache.rb
54
+ summary: Similar to Rails' built in MemoryStore, but adds size limit and expiration.
55
+ test_files: []
56
+
@@ -1,6 +0,0 @@
1
- === 1.0.0 / 2009-04-23
2
-
3
- * 1 major enhancement
4
-
5
- * Birthday!
6
-
@@ -1,7 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- bin/local_cache
6
- lib/local_cache.rb
7
- test/test_local_cache.rb
data/README.txt DELETED
@@ -1,54 +0,0 @@
1
- = local_cache
2
-
3
- http://www.crankapps.com
4
-
5
- == DESCRIPTION:
6
-
7
- A local in memory cache with max size and expiration.
8
-
9
- Can be used as a Rails cache too. In environment.rb:
10
-
11
- require 'local_cache'
12
- ActionController::Base.cache_store = :local_cache
13
- Activerecordtosdb.cache_store = ActionController::Base.cache_store
14
-
15
- == FEATURES/PROBLEMS:
16
-
17
- * FIX (list of features or problems)
18
-
19
- == SYNOPSIS:
20
-
21
- FIX (code sample of usage)
22
-
23
- == REQUIREMENTS:
24
-
25
- * FIX (list of requirements)
26
-
27
- == INSTALL:
28
-
29
- sudo gem install activesupport
30
-
31
- == LICENSE:
32
-
33
- (The MIT License)
34
-
35
- Copyright (c) 2009 FIX
36
-
37
- Permission is hereby granted, free of charge, to any person obtaining
38
- a copy of this software and associated documentation files (the
39
- 'Software'), to deal in the Software without restriction, including
40
- without limitation the rights to use, copy, modify, merge, publish,
41
- distribute, sublicense, and/or sell copies of the Software, and to
42
- permit persons to whom the Software is furnished to do so, subject to
43
- the following conditions:
44
-
45
- The above copyright notice and this permission notice shall be
46
- included in all copies or substantial portions of the Software.
47
-
48
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/local_cache.rb'
6
-
7
- Hoe.new('local_cache', ActiveSupport::Cache::LocalCache::VERSION) do |p|
8
- p.rubyforge_name = 'spacegems' # if different than lowercase project name
9
- p.developer('Travis Reeder', 'travis@crankapps.com')
10
- end
11
-
12
- # vim: syntax=Ruby
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- abort "you need to write me"
@@ -1,8 +0,0 @@
1
- require "test/unit"
2
- require "local_cache"
3
-
4
- class TestLocalCache < Test::Unit::TestCase
5
- def test_sanity
6
- flunk "write tests or I will kneecap you"
7
- end
8
- end