local_cache 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +7 -0
- data/README.txt +54 -0
- data/Rakefile +12 -0
- data/bin/local_cache +3 -0
- data/lib/local_cache.rb +71 -0
- data/test/test_local_cache.rb +8 -0
- metadata +81 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,54 @@
|
|
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
ADDED
@@ -0,0 +1,12 @@
|
|
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
ADDED
data/lib/local_cache.rb
ADDED
@@ -0,0 +1,71 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: local_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Reeder
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-23 00:00:00 -07:00
|
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
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- History.txt
|
41
|
+
- Manifest.txt
|
42
|
+
- README.txt
|
43
|
+
files:
|
44
|
+
- History.txt
|
45
|
+
- Manifest.txt
|
46
|
+
- README.txt
|
47
|
+
- Rakefile
|
48
|
+
- bin/local_cache
|
49
|
+
- lib/local_cache.rb
|
50
|
+
- test/test_local_cache.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://www.crankapps.com
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --main
|
58
|
+
- README.txt
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: spacegems
|
76
|
+
rubygems_version: 1.3.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A local in memory cache with max size and expiration
|
80
|
+
test_files:
|
81
|
+
- test/test_local_cache.rb
|