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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd7de71e70376b7fddaf6082f9136bc66e98e3e6
4
- data.tar.gz: c349e392443f65fa59c5c55259af48c8afd10eec
3
+ metadata.gz: 1f93dbc8d8b5f2e3d2923d3c9000c8bd20318b6d
4
+ data.tar.gz: b3145f6b8c78876b7370d644fe2118979bf6a499
5
5
  SHA512:
6
- metadata.gz: 6bc46327213cb45af8cd9890402e1936e7be7771b9f405cfb0693e91263c8815cef38df31e4f08af956bb7536a942e641d5982c0516d9a09be7296861642f7ac
7
- data.tar.gz: a96427c27f0a4ae159be068a1b76636da792096b77bc48b731d9a72896cd1c75e9713090c54fd00d7a91a23e02c3651a84997ad97d5c88e9bae20b07a32e624d
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
- cache = CacheBuilder.with(Cache).set_expiry('3s').set_max(500).set_refresh(Proc.new { |key| key + 100 }).build
36
-
37
- cache.put(1, 5)
38
- cache.get(1) # will return 5
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 leftover files on exit of program
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
- For more on what kind of strings are understood as times, [click here](https://github.com/jmettraux/rufus-scheduler/blob/two/README.rdoc#the-time-strings-understood-by-rufus-scheduler).
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
 
@@ -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
@@ -39,6 +39,11 @@ class CacheBuilder
39
39
  return self
40
40
  end
41
41
 
42
+ def set_post_get(proc)
43
+ @cache.post_get = proc
44
+ return self
45
+ end
46
+
42
47
  # Returns the newly created cache
43
48
  def build
44
49
  @cache.create_store
@@ -44,6 +44,7 @@ class FileCache < Cache
44
44
  if(@keys[key]) == nil
45
45
  return nil
46
46
  end
47
+ perform_post_get(key)
47
48
  return Marshal.load(File.read(File.join(store, @keys[key])))
48
49
  end
49
50
 
@@ -1,3 +1,3 @@
1
1
  module Libcache
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
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.1
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-21 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler