my_stuff-cache 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +13 -0
- data/README.rdoc +28 -0
- data/lib/my_stuff/cache/base.rb +25 -5
- data/lib/my_stuff/cache/memcached_cache.rb +5 -4
- data/lib/my_stuff/cache/memory_cache.rb +3 -2
- data/lib/my_stuff/cache/null_cache.rb +1 -1
- data/lib/my_stuff/cache.rb +1 -2
- metadata +23 -29
data/COPYING
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2011-present, Fred Emmott <copyright@fredemmott.co.uk>
|
2
|
+
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
purpose with or without fee is hereby granted, provided that the above
|
5
|
+
copyright notice and this permission notice appear in all copies.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
8
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
10
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
A simple caching library, optimised for:
|
4
|
+
* ease of use
|
5
|
+
* multigets
|
6
|
+
* delete/set rather than expire semantics
|
7
|
+
|
8
|
+
= Usage
|
9
|
+
|
10
|
+
See examples/.
|
11
|
+
|
12
|
+
= Getting it
|
13
|
+
|
14
|
+
git clone git://github.com/fredemmott/my_stuff-cache.git
|
15
|
+
|
16
|
+
or
|
17
|
+
|
18
|
+
gem install my_stuff-cache
|
19
|
+
|
20
|
+
= Backends
|
21
|
+
|
22
|
+
* null (no caching)
|
23
|
+
* memory (in the MyStuff::Cache object itself)
|
24
|
+
* memcached
|
25
|
+
|
26
|
+
= Copying
|
27
|
+
|
28
|
+
See the COPYING file.
|
data/lib/my_stuff/cache/base.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2011-present Fred Emmott.
|
1
|
+
# Copyright 2011-present Fred Emmott. See COPYING file.
|
2
2
|
|
3
3
|
module MyStuff
|
4
4
|
module Cache
|
@@ -9,6 +9,11 @@ module MyStuff
|
|
9
9
|
# - MyStuff::Cache::MemoryCache
|
10
10
|
# - MyStuff::Cache::MemcachedCache
|
11
11
|
class Base
|
12
|
+
attr_reader :logger
|
13
|
+
def initialize options = {}
|
14
|
+
@logger = options[:logger] || create_logger
|
15
|
+
end
|
16
|
+
|
12
17
|
# Try to get ids from cache, falling back to the given block.
|
13
18
|
#
|
14
19
|
# See #get_with_multi_fallback for documentation.
|
@@ -49,10 +54,12 @@ module MyStuff
|
|
49
54
|
data = Hash[ids.zip(get(ids.map{|x| key_pattern % x}))]
|
50
55
|
end
|
51
56
|
misses = data.select{|k,v| !v}.keys
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
57
|
+
unless misses.empty?
|
58
|
+
from_fallback = fallback.call(misses)
|
59
|
+
Hash[misses.zip(from_fallback)].each do |k,v|
|
60
|
+
to_cache[key_pattern % k] = v
|
61
|
+
data[k] = v;
|
62
|
+
end
|
56
63
|
end
|
57
64
|
set(to_cache) unless to_cache.empty? || !options[:update_cache]
|
58
65
|
ids.map{|k| data[k]}
|
@@ -80,6 +87,19 @@ module MyStuff
|
|
80
87
|
def set values, options = {}
|
81
88
|
raise NotImplementedError.new
|
82
89
|
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def create_logger
|
94
|
+
# Use MyStuff::Logger if it's already loaded; fall back to standard
|
95
|
+
# Logger
|
96
|
+
if (MyStuff.const_get(:Logger) rescue nil)
|
97
|
+
MyStuff::Logger.new
|
98
|
+
else
|
99
|
+
require 'logger'
|
100
|
+
Logger.new(STDOUT)
|
101
|
+
end
|
102
|
+
end
|
83
103
|
end
|
84
104
|
end
|
85
105
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2011-present Fred Emmott.
|
1
|
+
# Copyright 2011-present Fred Emmott. See COPYING file.
|
2
2
|
|
3
3
|
autoload :Memcached, 'memcached'
|
4
4
|
|
@@ -11,8 +11,9 @@ module MyStuff
|
|
11
11
|
# Create an instance of this cache.
|
12
12
|
#
|
13
13
|
# +memcached+ is a Memcached object, provided by the memcached gem.
|
14
|
-
def initialize memcached
|
14
|
+
def initialize memcached, options = {}
|
15
15
|
@mc = memcached
|
16
|
+
super options
|
16
17
|
end
|
17
18
|
|
18
19
|
def get keys, options = {}
|
@@ -20,7 +21,7 @@ module MyStuff
|
|
20
21
|
results = @mc.get(keys) unless keys.empty?
|
21
22
|
keys.map{|k| results[k]}
|
22
23
|
rescue Memcached::Error => e
|
23
|
-
|
24
|
+
logger.warn e.inspect
|
24
25
|
[nil] * keys.size
|
25
26
|
end
|
26
27
|
end
|
@@ -32,7 +33,7 @@ module MyStuff
|
|
32
33
|
@mc.set(k, v, options[:ttl])
|
33
34
|
end
|
34
35
|
rescue Memcached::Error => e
|
35
|
-
|
36
|
+
logger.warn e
|
36
37
|
end
|
37
38
|
end
|
38
39
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2011-present Fred Emmott.
|
1
|
+
# Copyright 2011-present Fred Emmott. See COPYING file.
|
2
2
|
|
3
3
|
module MyStuff
|
4
4
|
module Cache
|
@@ -8,8 +8,9 @@ module MyStuff
|
|
8
8
|
# this means that it won't be shared between all requests in a
|
9
9
|
# Passenger or pool-based setup.
|
10
10
|
class MemoryCache < MyStuff::Cache::Base
|
11
|
-
def initialize
|
11
|
+
def initialize options = {}
|
12
12
|
@cache = Hash.new
|
13
|
+
super options
|
13
14
|
end
|
14
15
|
|
15
16
|
def get keys, options = {}
|
data/lib/my_stuff/cache.rb
CHANGED
metadata
CHANGED
@@ -1,59 +1,53 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_stuff-cache
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Fred Emmott
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2011-08-15 00:00:00 Z
|
12
|
+
date: 2011-09-04 00:00:00.000000000Z
|
14
13
|
dependencies: []
|
15
|
-
|
16
14
|
description: A thin, multi-backend caching library
|
17
|
-
email:
|
15
|
+
email:
|
18
16
|
- mail@fredemmott.co.uk
|
19
17
|
executables: []
|
20
|
-
|
21
18
|
extensions: []
|
22
|
-
|
23
19
|
extra_rdoc_files: []
|
24
|
-
|
25
|
-
|
26
|
-
-
|
20
|
+
files:
|
21
|
+
- COPYING
|
22
|
+
- README.rdoc
|
27
23
|
- lib/my_stuff/cache/base.rb
|
28
24
|
- lib/my_stuff/cache/memcached_cache.rb
|
25
|
+
- lib/my_stuff/cache/memory_cache.rb
|
29
26
|
- lib/my_stuff/cache/null_cache.rb
|
30
27
|
- lib/my_stuff/cache.rb
|
31
28
|
homepage: https://github.com/fredemmott/my_stuff-cache
|
32
|
-
licenses:
|
33
|
-
|
29
|
+
licenses:
|
30
|
+
- ISC
|
34
31
|
post_install_message:
|
35
32
|
rdoc_options: []
|
36
|
-
|
37
|
-
require_paths:
|
33
|
+
require_paths:
|
38
34
|
- lib
|
39
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
36
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
version:
|
45
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
42
|
none: false
|
47
|
-
requirements:
|
48
|
-
- -
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version:
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
51
47
|
requirements: []
|
52
|
-
|
53
48
|
rubyforge_project:
|
54
49
|
rubygems_version: 1.8.6
|
55
50
|
signing_key:
|
56
51
|
specification_version: 3
|
57
52
|
summary: MyStuff.ws's caching library
|
58
53
|
test_files: []
|
59
|
-
|