libcache 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -29
- data/lib/libcache/cache.rb +10 -1
- data/lib/libcache/cache_builder.rb +5 -0
- data/lib/libcache/file_cache.rb +1 -0
- data/lib/libcache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f93dbc8d8b5f2e3d2923d3c9000c8bd20318b6d
|
4
|
+
data.tar.gz: b3145f6b8c78876b7370d644fe2118979bf6a499
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdcc1bd213388017f78ef50c2e0ce50ff68ed4701ffd6e7de408a164f5b120819c55d830d60d42043b7e7933b8dd4bf6a517193c1fc0066c4b89fe76dbcc89ef
|
7
|
+
data.tar.gz: c16a2b9e3d2ed9b1ebb24f27b21edc9e836f2e2ed392f49d122c99ba121820d1e46b8a5f587f34f4a1080c2931bea73e620dec5f074b2e517e3bdf5b86666ee4
|
data/README.md
CHANGED
@@ -28,48 +28,32 @@ Or install it yourself as:
|
|
28
28
|
$ gem install libcache
|
29
29
|
|
30
30
|
## Usage
|
31
|
-
|
32
|
-
For an in memory Cache with an expiry time of 3 seconds, store location at 'foo\bar', a max size of 500, and refresh method where 100 is added to the key (of course more sophisticated value retrieving operations will replace this method). Of course these additions are optional and configurable. The simplest form of an in-memory cache is ```CacheBuilder.with(Cache).build```
|
31
|
+
For an in memory Cache with an expiry time of 3 seconds, a max size of 500, and refresh method where 100 is added to the key (of course more sophisticated value retrieving operations will replace this method), and a function set_post_get which defines a function to be executed after the retrieval of a key. The These additions are optional and configurable. The simplest form of an in-memory cache is 'CacheBuilder.with(Cache).build'
|
33
32
|
```ruby
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
cache.
|
39
|
-
sleep 4 # note that this is more than the expiry time
|
40
|
-
cache.get(1) # will return 105 as the data has been refreshed
|
41
|
-
cache.exists?(1) # will return true. if there is no set_refresh method provided then it will return false
|
42
|
-
|
43
|
-
cache.put("key123", [1,2,true])
|
44
|
-
pp cache.get("key123") # prints '[1, 2, true]' preserves type
|
45
|
-
|
46
|
-
# delete all data on exit of program
|
47
|
-
at_exit do
|
48
|
-
cache.invalidate_all
|
49
|
-
end
|
50
|
-
|
33
|
+
cache = CacheBuilder.with(Cache).set_expiry('3s').set_max(500).set_refresh(lambda { |key| key + 100 }).set_post_get(lambda { |*key| puts "Retrieved #{key}!" }).build
|
34
|
+
```
|
35
|
+
...or for an file-based Cache with an expiry time of 3 seconds, store location at 'foo\bar', and refresh method where 100 is added to the key, and a function set_post_get which defines a function to be executed after the retrieval of a key. Of course these additions are optional and configurable. The only thing that is non-removable is the ```set_store``` method. The simplest form of a File cache is 'CacheBuilder.with(FileCache).set_store('foo\bar').build'
|
36
|
+
```ruby
|
37
|
+
cache = CacheBuilder.with(FileCache).set_store('foo\bar').set_expiry('3s').set_max(500).set_refresh(lambda { |key| key + 100 }).set_post_get(lambda { |*key| puts "Retrieved #{key}!" }).build
|
51
38
|
```
|
52
|
-
|
53
|
-
For an file-based Cache with an expiry time of 3 seconds, store location at 'foo\bar', and refresh method where 100 is added to the key (of course more sophisticated value retrieving operations will replace this method). Of course these additions are optional and configurable. The only thing that is non-removable is the ```set_store``` method.
|
54
39
|
```ruby
|
55
|
-
cache = CacheBuilder.with(FileCache).set_store('foo\bar').set_expiry('3s').set_max(500).set_refresh(Proc.new { |key| key + 100 }).build
|
56
|
-
|
57
40
|
cache.put(1, 5)
|
58
|
-
cache.get(1) # will return 5
|
41
|
+
cache.get(1) # will return 5, also prints "Retrieved [1]!" to the console, as per the function defined in the set_post_get method
|
59
42
|
sleep 4 # note that this is more than the expiry time
|
60
|
-
cache.get(1) # will return 105 as the data has been refreshed
|
43
|
+
cache.get(1) # will return 105 as the data has been refreshed, also prints "Retrieved [1]!"
|
61
44
|
cache.exists?(1) # will return true. if there is no set_refresh method provided then it will return false
|
62
45
|
|
63
46
|
cache.put("key123", [1,2,true])
|
64
|
-
pp cache.get("key123") # prints '[1, 2, true]' preserves type
|
47
|
+
pp cache.get("key123") # prints '[1, 2, true]' preserves type, prints "Retrieved ["key123"]!"
|
65
48
|
|
66
|
-
# delete all
|
49
|
+
# delete all data on exit of program
|
50
|
+
# if the file cache was used, deletes all left over files. #cache.invalidate_all can be called at any point in runtime.
|
67
51
|
at_exit do
|
68
52
|
cache.invalidate_all
|
69
53
|
end
|
70
|
-
```
|
71
54
|
|
72
|
-
|
55
|
+
```
|
56
|
+
For more on what kind of strings are understood as times, like "3s", [click here](https://github.com/jmettraux/rufus-scheduler/blob/two/README.rdoc#the-time-strings-understood-by-rufus-scheduler).
|
73
57
|
|
74
58
|
## Development
|
75
59
|
|
data/lib/libcache/cache.rb
CHANGED
@@ -3,7 +3,7 @@ require 'pp'
|
|
3
3
|
|
4
4
|
class Cache
|
5
5
|
|
6
|
-
attr_accessor :expiry_time, :refresh, :store, :max_size
|
6
|
+
attr_accessor :expiry_time, :refresh, :store, :max_size, :post_get
|
7
7
|
|
8
8
|
# Creates a basic Cache with the UTC timezone
|
9
9
|
def initialize
|
@@ -53,6 +53,7 @@ class Cache
|
|
53
53
|
if(@cache[key]) == nil
|
54
54
|
return nil
|
55
55
|
end
|
56
|
+
perform_post_get(key)
|
56
57
|
return @cache[key]
|
57
58
|
end
|
58
59
|
|
@@ -74,6 +75,14 @@ class Cache
|
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
78
|
+
# Performs a function after a key has been summoned from the cache
|
79
|
+
# @param [String] key The key value used to identify an object in the cache
|
80
|
+
def perform_post_get(key)
|
81
|
+
unless post_get.nil?
|
82
|
+
post_get.call(key)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
77
86
|
# @return [Boolean] Checks if the cache has a refresh method
|
78
87
|
def has_refresh?
|
79
88
|
return refresh == nil
|
data/lib/libcache/file_cache.rb
CHANGED
data/lib/libcache/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libcache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- silk8192
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|