libcache 0.3 → 0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1745f8dfa5166c285d38f5a9303350590e1aae2f
4
- data.tar.gz: e54f0dabd6ac9874078e89494d3ff32a3b79e53a
3
+ metadata.gz: e8612eef2a81c246498e1754292a22ae0f4dbda6
4
+ data.tar.gz: 7cc008ff91ef841e8a98d9a9113616f31047aca4
5
5
  SHA512:
6
- metadata.gz: a0bd90c60a77bfed598db5f7a707fb15fa9c06cf290d320fa44b6a2d7025fba30cfd87bddb65ed92ada94b95c1147872321da4f52f8029a13b63123dffd29a69
7
- data.tar.gz: e065672af43bb05078e505a1469ec6c440edee36650156d7718c9d4a4d54c995322de8e657830bb6010f6659913dffbfae5ae8a658dc430f561e612427bf7dce
6
+ metadata.gz: c79778ceb2c0c8e2ef1c29df41e6cf1ec9941d09624f0a884eb70c81d3dea2ebd039d4f271197b22d1fab71628f7221ce9f58d27c2f915e04f5e436440a4a038
7
+ data.tar.gz: 1b0d07b38f7926a47dffa619ea2232d4f7c87a5146d16d101fe533477e91cb0457d7f2aefcbed57724dbdf47e149c8732bbfd7e1e9136bc56d977445ae254505
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Libcache [![Build Status](https://travis-ci.org/silk8192/libcache.svg?branch=master)](https://travis-ci.org/silk8192/libcache) [![Gem Version](https://badge.fury.io/rb/libcache.svg)](https://badge.fury.io/rb/libcache)
2
2
 
3
- A simple caching library that provides flexible and powerful caching features such as in-memory and file based caching similar to Guava's Caching system.
3
+ A simple caching library that provides flexible and powerful caching features such as in-memory and file based caching similar to Guava's Caching system. [Docs.](http://www.rubydoc.info/gems/libcache)
4
4
 
5
5
  ## Features
6
6
 
@@ -11,7 +11,6 @@ A simple caching library that provides flexible and powerful caching features su
11
11
  * Allows custom refresh functions for reloading expensive data once it has been discarded
12
12
 
13
13
 
14
-
15
14
  ## Installation
16
15
 
17
16
  Add this line to your application's Gemfile:
@@ -43,7 +42,7 @@ cache.exists?(1) # will return true. if there is no set_refresh method provided
43
42
 
44
43
  # delete all data on exit of program
45
44
  at_exit do
46
- cache.invalidateAll
45
+ cache.invalidate_all
47
46
  end
48
47
 
49
48
  ```
@@ -61,7 +60,7 @@ cache.exists?(1) # will return true. if there is no set_refresh method provided
61
60
 
62
61
  # delete all leftover files on exit of program
63
62
  at_exit do
64
- cache.invalidateAll
63
+ cache.invalidate_all
65
64
  end
66
65
  ```
67
66
 
@@ -21,21 +21,28 @@ class Cache
21
21
  # @param [String] key The key value used to identify an object in the cache
22
22
  # @param [Object] value The object to be placed in the cache
23
23
  def put(key, value)
24
- # removed oldest entry if max_size is approached
25
- if max_size != nil
26
- if @cache.size >= max_size - 1
27
- key, value = @time_tracker.values.sort {|v| Time.now - v }.reverse.first
28
- invalidate(key)
29
- @time_tracker.delete(key)
30
- end
31
- @time_tracker[key] = Time.now
32
- end
33
24
  @cache[key] = value
34
25
  if expiry_time != nil
35
26
  @scheduler.in expiry_time, :blocking => true do
36
27
  invalidate key
37
28
  end
38
29
  end
30
+ check_expiration(key)
31
+ end
32
+
33
+ private def check_expiration(key)
34
+ # removed oldest entry if max_size is approached
35
+ @time_tracker[key] = Time.now
36
+ if max_size != nil
37
+ if get_size >= max_size
38
+ n = get_size - max_size
39
+ keys = @time_tracker.sort_by{|k,v| Time.now - v }.reverse.first n
40
+ keys.each do |keyz, valuez|
41
+ invalidate(keyz)
42
+ @time_tracker.delete(keyz)
43
+ end
44
+ end
45
+ end
39
46
  end
40
47
 
41
48
  # Gets the object that corresponds with the key
@@ -43,6 +50,9 @@ class Cache
43
50
  # @return [Object] The object that corresponds with the key
44
51
  def get(key)
45
52
  check_refresh(key)
53
+ if(@cache[key]) == nil
54
+ return nil
55
+ end
46
56
  return @cache[key]
47
57
  end
48
58
 
@@ -70,7 +80,7 @@ class Cache
70
80
  end
71
81
 
72
82
  def get_size
73
- return @cache.size
83
+ return @cache.length
74
84
  end
75
85
 
76
86
  # Deletes a key-value pair from the cache
@@ -80,7 +90,7 @@ class Cache
80
90
  end
81
91
 
82
92
  # Clears all items in the cache
83
- def invalidateAll
93
+ def invalidate_all
84
94
  @cache.clear
85
95
  end
86
96
 
@@ -23,42 +23,40 @@ class FileCache < Cache
23
23
  def put(key, value)
24
24
  raise InvalidKey unless key.is_a? String
25
25
  raise InvalidKey unless key =~ /\A[a-zA-Z0-9_-]+\z/
26
- if max_size != nil
27
- if @cache.size >= max_size - 1
28
- key, value = @time_tracker.values.sort {|v| Time.now - v }.reverse.first
29
- invalidate(key)
30
- @time_tracker.delete(key)
31
- end
32
- @time_tracker[key] = Time.now
33
- end
34
- @keys[key] = Digest::MD5.hexdigest(key) + Time.now.to_i.to_s
35
- @cache[key] = value
26
+ @keys[key] = @keys.size.to_s
36
27
  File.open(File.join(store, @keys[key]), 'w') do |f|
37
- f.write(value)
28
+ f.write(Marshal.dump(value))
38
29
  end
39
- @scheduler.in expiry_time, :blocking => true do
40
- invalidate key
30
+ @cache[key] = value
31
+ if expiry_time != nil
32
+ @scheduler.in expiry_time, :blocking => true do
33
+ invalidate key
34
+ end
41
35
  end
36
+ check_expiration(key)
42
37
  end
43
38
 
44
39
  # Gets the object that corresponds with the key that is read from the filesystem
45
40
  # @param [String] key The key value used to identify an object in the cache
46
41
  # @return [Object] The object that corresponds with the key
47
42
  def get(key)
48
- refresh
49
- return File.read(File.join(store, @keys[key]))
43
+ check_refresh(key)
44
+ if(@keys[key]) == nil
45
+ return nil
46
+ end
47
+ return Marshal.load(File.read(File.join(store, @keys[key])))
50
48
  end
51
49
 
52
50
  # Deletes a key-value pair from the cache and store directory
53
51
  # @param [String] key The key value used to identify an object in the cache
54
52
  def invalidate(key)
55
53
  super
56
- @keys.delete key
57
54
  File.delete(File.join(store, @keys[key]))
55
+ @keys.delete key
58
56
  end
59
57
 
60
58
  # Clears all items in the cache and the cached files in the store directory
61
- def invalidateAll
59
+ def invalidate_all
62
60
  super
63
61
  Dir.foreach(store) do |f|
64
62
  File.delete(File.join(store, f)) if f != '.' && f != '..'
@@ -1,3 +1,3 @@
1
1
  module Libcache
2
- VERSION = "0.3"
2
+ VERSION = "0.4"
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.3'
4
+ version: '0.4'
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-15 00:00:00.000000000 Z
11
+ date: 2017-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler