persistent-cache 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,10 +4,7 @@ Persistent cache behaves like a hash, with a pluggable back-end. Currently sqlit
4
4
 
5
5
  Values in the cache have a default freshness period of 15465600 ms. This can be configured in the cache initializer. Setting fresh = nil indicates that data remains fresh for-ever. Each user of the cache may have his own independent freshness value. Not though that accessing a stale entry deletes it from the cache. You can use timestamp?(key) to read the timestamp of an entry. If stale data is requested from the cache, nil is returned. Data is marshalled before storage. If a key is not found in the cache, nil is returned. Setting the value of a key in the cache to nil deletes the entry. If required, creation time of an entry can be specified using set(key, value, timestamp)
6
6
 
7
- Note that when using a back-end that requires marshalling (e.g. sqlite) the string encoding for []= and [] needs to be the same (e.g. UTF-8, US-ASCII, etc.) If the coding does not match, [] will not be able to find the entry during lookup and will return nil. The easiest way to accomplish this with a ruby script is by adding the following shell directive at the top of your main.rb
8
- ```
9
- # encoding: utf-8
10
- ```
7
+ Note that when using a back-end that requires marshalling (e.g. sqlite) the string encoding for []= and [] needs to be the same (e.g. UTF-8, US-ASCII, etc.) If the coding does not match, [] will not be able to find the entry during lookup and will return nil. See the section on 'Encoding' below for more detail.
11
8
 
12
9
  This gem is sponsored by Hetzner (Pty) Ltd - http://hetzner.co.za
13
10
 
@@ -89,14 +86,24 @@ Or install it yourself as:
89
86
 
90
87
  cache = Persistent::Cache.new("cache-name", nil, Persistent::Cache::STORAGE_RAM)
91
88
 
89
+ ## Encoding
90
+
91
+ Note that when using a back-end that requires marshalling (e.g. sqlite) the string encoding for []= and [] needs to be the same (e.g. UTF-8, US-ASCII, etc.) If the coding does not match, [] will not be able to find the entry during lookup and will return nil. See the section on 'Encoding' below for more detail.
92
+
93
+ The easiest way to accomplish this with a ruby script is by adding the following shell directive at the top of your main.rb
94
+ # encoding: utf-8
95
+
96
+ If you'd like persistent cache to rather store keys using an encoding of your preference, after initialization set the encoding explicitly using:
97
+ cache.encoding = Encoding::UTF_8
98
+
99
+ ## Contributing
100
+
92
101
  Please send feedback and comments to the authors at:
93
102
 
94
103
  Wynand van Dyk <wynand.van.dyk@hetzner.co.za>
95
104
 
96
105
  Ernst van Graan <ernst.van.graan@hetzner.co.za>
97
106
 
98
- ## Contributing
99
-
100
107
  1. Fork it
101
108
  2. Create your feature branch (`git checkout -b my-new-feature`)
102
109
  3. Commit your changes (`git commit -am 'Added some feature'`)
@@ -1,5 +1,5 @@
1
1
  module Persistent
2
2
  class Cache
3
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
4
4
  end
5
5
  end
@@ -16,6 +16,7 @@ module Persistent
16
16
  attr_accessor :storage_details
17
17
  attr_accessor :storage
18
18
  attr_accessor :fresh
19
+ attr_accessor :encoding
19
20
 
20
21
  def initialize(storage_details, fresh = FRESH, storage = STORAGE_SQLITE)
21
22
  raise ArgumentError.new("No storage details provided") if storage_details.nil? or storage_details == ""
@@ -49,6 +50,15 @@ module Persistent
49
50
  lookup_key(key)
50
51
  end
51
52
 
53
+ def lookup_by_key_value(key)
54
+ if @storage.keys
55
+ @storage.keys.each do |k|
56
+ return lookup_key(k) if k == key
57
+ end
58
+ end
59
+ return nil
60
+ end
61
+
52
62
  def each(&block)
53
63
  keys.each do |key|
54
64
  yield key, lookup_key(key)
@@ -76,11 +86,13 @@ module Persistent
76
86
  private
77
87
 
78
88
  def save_key_value_pair(key, value, timestamp = nil)
89
+ key = key.encode!(@encoding) if @encoding
79
90
  @storage.delete_entry(key)
80
91
  @storage.save_key_value_pair(key, value, timestamp)
81
92
  end
82
93
 
83
94
  def lookup_key(key)
95
+ key = key.encode!(@encoding) if @encoding
84
96
  result = @storage.lookup_key(key)
85
97
  return nil if nil_result?(result)
86
98
  return nil if stale_entry?(key, result)
@@ -100,6 +112,7 @@ module Persistent
100
112
  end
101
113
 
102
114
  def delete_entry(key)
115
+ key = key.encode!(@encoding) if @encoding
103
116
  @storage.delete_entry(key)
104
117
  end
105
118
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persistent-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-06-04 00:00:00.000000000 Z
13
+ date: 2015-06-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec