memmo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -0
- data/README.md +44 -0
- data/UNLICENSE +24 -0
- data/lib/memmo/version.rb +3 -0
- data/lib/memmo.rb +50 -0
- data/memmo.gemspec +13 -0
- data/rakefile +8 -0
- data/test/memmo_test.rb +68 -0
- metadata +55 -0
data/.gitignore
ADDED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
Memmo
|
2
|
+
=====
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/educabilia/memmo.png?branch=master)](https://travis-ci.org/educabilia/memmo)
|
5
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/educabilia/memmo)
|
6
|
+
|
7
|
+
Memmo is a small library for creating key-based, in-memory caches in Ruby.
|
8
|
+
|
9
|
+
It features thread safety and TTLs.
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
|
16
|
+
require "memmo"
|
17
|
+
|
18
|
+
$memmo = Memmo.new
|
19
|
+
|
20
|
+
$memmo.register(:popular_posts, ttl: 60) do
|
21
|
+
# Somewhat expensive query
|
22
|
+
Post.find(...)
|
23
|
+
end
|
24
|
+
|
25
|
+
# This call will trigger a miss and will run our expensive query.
|
26
|
+
$memmo[:popular_posts].each do |post|
|
27
|
+
puts(post)
|
28
|
+
end
|
29
|
+
|
30
|
+
# This call will be fast for the next 60 seconds.
|
31
|
+
$memmo[:popular_posts]
|
32
|
+
```
|
33
|
+
|
34
|
+
Test environments
|
35
|
+
-----------------
|
36
|
+
|
37
|
+
It's common to avoid caches when testing your application. For this
|
38
|
+
purpose you can disable Memmo entirely:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require "test/unit"
|
42
|
+
|
43
|
+
Memmo.enabled = false
|
44
|
+
```
|
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/lib/memmo.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Memmo
|
2
|
+
def self.enabled
|
3
|
+
@enabled
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.enabled=(value)
|
7
|
+
@enabled = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@mutex = Mutex.new
|
12
|
+
@loaders = {}
|
13
|
+
@cache = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def register(name, options = {}, &block)
|
17
|
+
@mutex.synchronize do
|
18
|
+
@loaders[name] = [options, block]
|
19
|
+
refresh(name)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def get(name)
|
24
|
+
return refresh(name)[1] unless Memmo.enabled
|
25
|
+
|
26
|
+
if !@cache.include?(name) || (@loaders[name][0][:ttl] && Time.now - @cache[name][0] > @loaders[name][0][:ttl])
|
27
|
+
@mutex.synchronize do
|
28
|
+
refresh(name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
@cache[name][1]
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear
|
36
|
+
@cache.clear
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](name)
|
40
|
+
get(name)
|
41
|
+
end
|
42
|
+
|
43
|
+
self.enabled = true
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def refresh(name)
|
48
|
+
@cache[name] = [Time.now, @loaders[name][1].call]
|
49
|
+
end
|
50
|
+
end
|
data/memmo.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path("lib/memmo/version", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "memmo"
|
5
|
+
s.version = Memmo::VERSION
|
6
|
+
s.summary = "In-memory, thread-safe caches supporting time-to-live."
|
7
|
+
s.authors = ["Educabilia", "Damian Janowski"]
|
8
|
+
s.email = ["opensource@educabilia.com", "djanowski@dimaion.com"]
|
9
|
+
s.homepage = "https://github.com/educabilia/memmo"
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
13
|
+
end
|
data/rakefile
ADDED
data/test/memmo_test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require_relative "../lib/memmo"
|
4
|
+
|
5
|
+
class MemmoTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@memmo = Memmo.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_miss
|
11
|
+
@memmo.register(:numbers) { [1, 2, 3] }
|
12
|
+
|
13
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_cache
|
17
|
+
calls = 0
|
18
|
+
|
19
|
+
@memmo.register(:numbers) do
|
20
|
+
calls += 1
|
21
|
+
|
22
|
+
[1, 2, 3]
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
26
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
27
|
+
assert_equal 1, calls
|
28
|
+
|
29
|
+
assert_equal [1, 2, 3], @memmo[:numbers]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_cache_ttl
|
33
|
+
calls = 0
|
34
|
+
|
35
|
+
@memmo.register(:numbers, ttl: 0.1) do
|
36
|
+
calls += 1
|
37
|
+
|
38
|
+
[1, 2, 3]
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
42
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
43
|
+
assert_equal 1, calls
|
44
|
+
|
45
|
+
sleep 0.2
|
46
|
+
|
47
|
+
assert_equal [1, 2, 3], @memmo.get(:numbers)
|
48
|
+
assert_equal 2, calls
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_can_be_disabled
|
52
|
+
Memmo.enabled = false
|
53
|
+
|
54
|
+
calls = 0
|
55
|
+
|
56
|
+
@memmo.register(:numbers, ttl: 10) do
|
57
|
+
calls += 1
|
58
|
+
end
|
59
|
+
|
60
|
+
@memmo.get(:numbers)
|
61
|
+
|
62
|
+
assert_equal 2, calls
|
63
|
+
end
|
64
|
+
|
65
|
+
def teardown
|
66
|
+
Memmo.enabled = true
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memmo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Educabilia
|
9
|
+
- Damian Janowski
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-01-09 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description:
|
16
|
+
email:
|
17
|
+
- opensource@educabilia.com
|
18
|
+
- djanowski@dimaion.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- README.md
|
25
|
+
- UNLICENSE
|
26
|
+
- lib/memmo.rb
|
27
|
+
- lib/memmo/version.rb
|
28
|
+
- memmo.gemspec
|
29
|
+
- rakefile
|
30
|
+
- test/memmo_test.rb
|
31
|
+
homepage: https://github.com/educabilia/memmo
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.23
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: In-memory, thread-safe caches supporting time-to-live.
|
55
|
+
test_files: []
|