appoxy-local_cache 1.1.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 +0 -0
- data/lib/local_cache.rb +75 -0
- metadata +54 -0
data/README.markdown
ADDED
File without changes
|
data/lib/local_cache.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module Cache
|
5
|
+
class LocalCache < Store
|
6
|
+
|
7
|
+
def initialize(size=1000, default_expires_in=60)
|
8
|
+
puts 'Creating new LocalCache'
|
9
|
+
@size = size
|
10
|
+
@default_expires_in = default_expires_in
|
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
|
+
#puts 'looking in cache for: ' + key.to_s
|
20
|
+
if @cache.has_key? key
|
21
|
+
expires = @cache[key][0]
|
22
|
+
#puts 'checking expired=' + key + ' at ' + expires.to_s + ' and now=' + Time.now.to_s
|
23
|
+
if expires - Time.now > 0
|
24
|
+
# puts 'returning from cache ' + @cache[key][1].inspect
|
25
|
+
# todo: implement LRU ordering
|
26
|
+
return @cache[key][1]
|
27
|
+
else
|
28
|
+
#puts 'expired'
|
29
|
+
delete(key)
|
30
|
+
end
|
31
|
+
else
|
32
|
+
# puts 'cache does not contain ' + key
|
33
|
+
end
|
34
|
+
return nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def put(key, val, seconds_to_store)
|
38
|
+
|
39
|
+
seconds_to_store = seconds_to_store || @default_expires_in
|
40
|
+
#puts 'seconds=' + seconds_to_store.to_s
|
41
|
+
@cache[key] = [Time.now+seconds_to_store, val]
|
42
|
+
@cache_list << key
|
43
|
+
while @cache.size > @size && @cache_list.size > 0
|
44
|
+
to_remove = @cache_list.pop
|
45
|
+
@cache.delete(to_remove) unless to_remove.nil?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def read(name, options = nil)
|
50
|
+
# puts 'read from localcache'
|
51
|
+
super
|
52
|
+
ret = get(name)
|
53
|
+
# puts 'ret.frozen=' + ret.frozen?.to_s
|
54
|
+
return ret
|
55
|
+
end
|
56
|
+
|
57
|
+
def write(name, value, options = nil)
|
58
|
+
super
|
59
|
+
put(name, value, options.nil? ? nil : options[:expires_in])
|
60
|
+
# puts 'write.frozen=' + value.frozen?.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
def delete(name, options = nil)
|
64
|
+
super
|
65
|
+
@cache.delete(name)
|
66
|
+
end
|
67
|
+
|
68
|
+
def delete_matched(matcher, options = nil)
|
69
|
+
super
|
70
|
+
raise "delete_matched not supported by LocalCache"
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: appoxy-local_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Reeder
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
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
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- lib/local_cache.rb
|
26
|
+
- README.markdown
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/appoxy/local_cache/
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options:
|
31
|
+
- --charset=UTF-8
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: "0"
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.2.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: Similar to Rails' built in MemoryStore, but adds size limit and expiration.
|
53
|
+
test_files: []
|
54
|
+
|