basiccache 0.0.13 → 0.0.15
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.
- checksums.yaml +4 -4
- data/Rakefile +9 -2
- data/basiccache.gemspec +1 -0
- data/lib/basiccache.rb +18 -2
- data/test/test_cache.rb +10 -6
- data/test/test_timecache.rb +10 -6
- metadata +17 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: da00ad80106e951e1156a2402576d9ce4dd4ee2c
|
|
4
|
+
data.tar.gz: f1196a82c291a8bf12e4e835a4f4af8b1182a474
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bdca6535cd46161ec82ffae4969fa388ab74176e1847acc372af60d0651f0a698677d807644ff01fa6ca9039e371f01a839f88a506e445964029a2e1bfc649e0
|
|
7
|
+
data.tar.gz: c1106f9c26269292180a3acd773ddd4146c5e4120042a4183c8458c47690edf7634a7ae151a1c10c456f7fbd0547e3ec2e5a86fc339bbb9fe22def1cd432feb4
|
data/Rakefile
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
require "bundler/gem_tasks"
|
|
2
2
|
require 'rake/testtask'
|
|
3
|
+
require 'rubocop/rake_task'
|
|
3
4
|
|
|
5
|
+
desc 'Run tests'
|
|
4
6
|
Rake::TestTask.new do |t|
|
|
5
7
|
t.libs << 'test'
|
|
6
8
|
end
|
|
7
9
|
|
|
8
|
-
desc
|
|
9
|
-
|
|
10
|
+
desc 'Run Rubocop on the gem'
|
|
11
|
+
Rubocop::RakeTask.new(:rubocop) do |task|
|
|
12
|
+
task.patterns = ['lib/*.rb', 'test/*.rb']
|
|
13
|
+
task.fail_on_error = true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
task :default => [:test, :rubocop, :build, :install]
|
|
10
17
|
|
data/basiccache.gemspec
CHANGED
data/lib/basiccache.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# This module provides a simple key/value cache for storing computation results
|
|
3
3
|
|
|
4
4
|
module BasicCache
|
|
5
|
-
VERSION = '0.0.
|
|
5
|
+
VERSION = '0.0.15'
|
|
6
6
|
|
|
7
7
|
class << self
|
|
8
8
|
##
|
|
@@ -73,6 +73,15 @@ module BasicCache
|
|
|
73
73
|
key ||= BasicCache.get_caller
|
|
74
74
|
@store.include? key.to_sym
|
|
75
75
|
end
|
|
76
|
+
|
|
77
|
+
##
|
|
78
|
+
# Retrieve cached value
|
|
79
|
+
|
|
80
|
+
def [](key = nil)
|
|
81
|
+
key ||= BasicCache.get_caller
|
|
82
|
+
fail KeyError, 'Key not cached' unless @store.include? key.to_sym
|
|
83
|
+
@store[key.to_sym]
|
|
84
|
+
end
|
|
76
85
|
end
|
|
77
86
|
|
|
78
87
|
##
|
|
@@ -96,9 +105,11 @@ module BasicCache
|
|
|
96
105
|
def cache(key = nil, &code)
|
|
97
106
|
key ||= BasicCache.get_caller
|
|
98
107
|
key = key.to_sym
|
|
108
|
+
# rubocop:disable AndOr
|
|
99
109
|
unless @store.include? key and Time.now - @store[key].stamp < @lifetime
|
|
100
110
|
@store[key] = @cache_item.new(Time.now, code.call)
|
|
101
111
|
end
|
|
112
|
+
# rubocop:enable AndOr
|
|
102
113
|
@store[key].value
|
|
103
114
|
end
|
|
104
115
|
|
|
@@ -108,8 +119,13 @@ module BasicCache
|
|
|
108
119
|
def include?(key = nil)
|
|
109
120
|
key ||= BasicCache.get_caller
|
|
110
121
|
key = key.to_sym
|
|
122
|
+
# rubocop:disable AndOr
|
|
111
123
|
@store.include? key and Time.now - @store[key].stamp < @lifetime
|
|
124
|
+
# rubocop:enable AndOr
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def [](key = nil)
|
|
128
|
+
super.value
|
|
112
129
|
end
|
|
113
130
|
end
|
|
114
131
|
end
|
|
115
|
-
|
data/test/test_cache.rb
CHANGED
|
@@ -2,11 +2,14 @@ require 'test/unit'
|
|
|
2
2
|
require 'benchmark'
|
|
3
3
|
require 'basiccache'
|
|
4
4
|
|
|
5
|
+
##
|
|
6
|
+
# Let's cache some stuff!
|
|
7
|
+
|
|
5
8
|
class BasicCacheTest < Test::Unit::TestCase
|
|
6
9
|
def compute(n)
|
|
7
10
|
(1..n).reduce { |a, e| a * e }
|
|
8
11
|
end
|
|
9
|
-
|
|
12
|
+
|
|
10
13
|
def test_creation
|
|
11
14
|
a = BasicCache.new
|
|
12
15
|
b = BasicCache::Cache.new
|
|
@@ -20,8 +23,8 @@ class BasicCacheTest < Test::Unit::TestCase
|
|
|
20
23
|
cache = BasicCache.new
|
|
21
24
|
cache.cache { compute(10) }
|
|
22
25
|
assert cache.store.include? :test_caching
|
|
23
|
-
assert_equal cache.store[:test_caching],
|
|
24
|
-
assert_equal cache.cache,
|
|
26
|
+
assert_equal cache.store[:test_caching], 3_628_800
|
|
27
|
+
assert_equal cache.cache, 3_628_800
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
def test_clearing
|
|
@@ -34,8 +37,9 @@ class BasicCacheTest < Test::Unit::TestCase
|
|
|
34
37
|
|
|
35
38
|
def test_speed_increase
|
|
36
39
|
cache = BasicCache.new
|
|
37
|
-
trials = 2.times.map
|
|
38
|
-
|
|
40
|
+
trials = 2.times.map do
|
|
41
|
+
Benchmark.measure { cache.cache { compute(50_000) } }.real
|
|
42
|
+
end
|
|
43
|
+
assert trials[0] > trials[1] * 1_000
|
|
39
44
|
end
|
|
40
45
|
end
|
|
41
|
-
|
data/test/test_timecache.rb
CHANGED
|
@@ -2,11 +2,14 @@ require 'test/unit'
|
|
|
2
2
|
require 'benchmark'
|
|
3
3
|
require 'basiccache'
|
|
4
4
|
|
|
5
|
+
##
|
|
6
|
+
# Lets test some timed caches!
|
|
7
|
+
|
|
5
8
|
class TimeCacheTest < Test::Unit::TestCase
|
|
6
9
|
def compute(n)
|
|
7
10
|
(1..n).reduce { |a, e| a * e }
|
|
8
11
|
end
|
|
9
|
-
|
|
12
|
+
|
|
10
13
|
def test_creation
|
|
11
14
|
a = BasicCache::TimeCache.new
|
|
12
15
|
assert_instance_of BasicCache::TimeCache, a
|
|
@@ -20,8 +23,8 @@ class TimeCacheTest < Test::Unit::TestCase
|
|
|
20
23
|
cache = BasicCache::TimeCache.new
|
|
21
24
|
cache.cache { compute(10) }
|
|
22
25
|
assert cache.store.include? :test_caching
|
|
23
|
-
assert_equal cache.store[:test_caching].value,
|
|
24
|
-
assert_equal cache.cache,
|
|
26
|
+
assert_equal cache.store[:test_caching].value, 3_628_800
|
|
27
|
+
assert_equal cache.cache, 3_628_800
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
def test_clearing
|
|
@@ -34,8 +37,10 @@ class TimeCacheTest < Test::Unit::TestCase
|
|
|
34
37
|
|
|
35
38
|
def test_speed_increase
|
|
36
39
|
cache = BasicCache::TimeCache.new
|
|
37
|
-
trials = 2.times.map
|
|
38
|
-
|
|
40
|
+
trials = 2.times.map do
|
|
41
|
+
Benchmark.measure { cache.cache { compute(50_000) } }.real
|
|
42
|
+
end
|
|
43
|
+
assert trials[0] > trials[1] * 1_000
|
|
39
44
|
end
|
|
40
45
|
|
|
41
46
|
def test_expiration
|
|
@@ -46,4 +51,3 @@ class TimeCacheTest < Test::Unit::TestCase
|
|
|
46
51
|
assert cache.include?(:test) == false
|
|
47
52
|
end
|
|
48
53
|
end
|
|
49
|
-
|
metadata
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: basiccache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.15
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Les Aker
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-11-
|
|
12
|
-
dependencies:
|
|
11
|
+
date: 2013-11-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rubocop
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
13
27
|
description: Allows an application to dynamically cache values and retrieve them later
|
|
14
28
|
email: me@lesaker.org
|
|
15
29
|
executables: []
|