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.
- data/README.markdown +26 -0
- data/lib/local_cache.rb +114 -71
- metadata +17 -42
- data/History.txt +0 -6
- data/Manifest.txt +0 -7
- data/README.txt +0 -54
- data/Rakefile +0 -12
- data/bin/local_cache +0 -3
- data/test/test_local_cache.rb +0 -8
data/README.markdown
ADDED
@@ -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
|
+
|
data/lib/local_cache.rb
CHANGED
@@ -1,71 +1,114 @@
|
|
1
|
-
require 'active_support'
|
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
|
-
ret
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
|
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.
|
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-
|
12
|
+
date: 2009-10-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
-
|
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
|
-
-
|
26
|
+
- README.markdown
|
51
27
|
has_rdoc: true
|
52
|
-
homepage: http://
|
28
|
+
homepage: http://github.com/appoxy/local_cache/
|
53
29
|
licenses: []
|
54
30
|
|
55
31
|
post_install_message:
|
56
32
|
rdoc_options:
|
57
|
-
- --
|
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:
|
76
|
-
rubygems_version: 1.3.
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
77
52
|
signing_key:
|
78
53
|
specification_version: 3
|
79
|
-
summary:
|
80
|
-
test_files:
|
81
|
-
|
54
|
+
summary: Similar to Rails' built in MemoryStore, but adds size limit and expiration.
|
55
|
+
test_files: []
|
56
|
+
|
data/History.txt
DELETED
data/Manifest.txt
DELETED
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
|
data/bin/local_cache
DELETED