mrcr-cache 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/mrcr/cache.rb +31 -5
- data/lib/mrcr/cache/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b7da641b1029613733d14a8823c39bdbcb1abe7
|
4
|
+
data.tar.gz: 6b7e4013b3074502cfbace458b58937d479a5a05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5091c9e32958be89b49fe40c0928258476daefe273962c3598763dfdabee4904901b73afe5e96d4e155f6d826344c04c39dbc9e9bc20d9f30a25b49d3a09a892
|
7
|
+
data.tar.gz: 6c7741baeca70826d5c7bc265819f929a234f46c2725141646dcb55ffa6e10a24f53cd650a641673e01fe14ef060c8385b427628c04ce2e84d4440e8d6d4b679
|
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/mrcr-cache.svg)](https://badge.fury.io/rb/mrcr-cache)
|
2
|
+
[![Build Status](https://travis-ci.org/merqlove/mrcr-cache.svg?branch=master)](https://travis-ci.org/merqlove/mrcr-cache)
|
3
|
+
|
1
4
|
# Mrcr::Cache
|
2
5
|
|
3
6
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mrcr/cache`. To experiment with that code, run `bin/console` for an interactive prompt.
|
data/lib/mrcr/cache.rb
CHANGED
@@ -11,7 +11,8 @@ module Mrcr
|
|
11
11
|
# extend Mrcr::Cache
|
12
12
|
#
|
13
13
|
# def heavy_computation(arg1, arg2)
|
14
|
-
# fetch_or_store(arg1
|
14
|
+
# fetch_or_store(arg1) { arg1 ^ arg2 }
|
15
|
+
# fetch(arg1, nil)
|
15
16
|
# end
|
16
17
|
# end
|
17
18
|
#
|
@@ -45,8 +46,22 @@ module Mrcr
|
|
45
46
|
# they are always the same instances, otherwise you introduce a memory leak
|
46
47
|
#
|
47
48
|
# @return [Object] block's return value (cached for subsequent calls with the same argument values)
|
48
|
-
def fetch_or_store(
|
49
|
-
cache.fetch_or_store(
|
49
|
+
def fetch_or_store(key, &block)
|
50
|
+
cache.fetch_or_store(key.hash, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Caches a result of the block evaluation
|
54
|
+
#
|
55
|
+
# @param [Array<Object>] args List of hashable objects
|
56
|
+
# @yield An arbitrary block
|
57
|
+
#
|
58
|
+
# @note beware Proc instance hashes are not equal, i.e. -> { 1 }.hash != -> { 1 }.hash,
|
59
|
+
# this means you shouldn't pass Procs in args unless you're sure
|
60
|
+
# they are always the same instances, otherwise you introduce a memory leak
|
61
|
+
#
|
62
|
+
# @return [Object] block's return value (cached for subsequent calls with the same argument values)
|
63
|
+
def fetch(key, default = nil)
|
64
|
+
cache.fetch(key.hash, default)
|
50
65
|
end
|
51
66
|
|
52
67
|
# Instance methods
|
@@ -57,8 +72,19 @@ module Mrcr
|
|
57
72
|
# @yield An arbitrary block
|
58
73
|
#
|
59
74
|
# @return [Object] block's return value
|
60
|
-
def fetch_or_store(
|
61
|
-
self.class.fetch_or_store(
|
75
|
+
def fetch_or_store(key, &block)
|
76
|
+
self.class.fetch_or_store(key, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Delegates call to the class-level method
|
80
|
+
#
|
81
|
+
# @param [Object] value List of hashable objects
|
82
|
+
# @param [Object] default List of hashable objects
|
83
|
+
# @yield An arbitrary block
|
84
|
+
#
|
85
|
+
# @return [Object] block's return value
|
86
|
+
def fetch(key, default = nil)
|
87
|
+
self.class.fetch(key, default)
|
62
88
|
end
|
63
89
|
end
|
64
90
|
end
|
data/lib/mrcr/cache/version.rb
CHANGED