persistent-cache 0.2.3 → 0.3.0

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.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ multidb
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
@@ -1,4 +1,5 @@
1
1
  require "sqlite3"
2
+ require "base64"
2
3
  require "eh/eh"
3
4
 
4
5
  module Persistent
@@ -19,21 +20,21 @@ module Persistent
19
20
  def save_key_value_pair(key, value, timestamp = nil)
20
21
  delete_entry(key)
21
22
  time_entry = timestamp.nil? ? Time.now.to_s : timestamp.to_s
22
- EH::retry!(:args => [Marshal.dump(key), Marshal.dump(value), time_entry]) do
23
- @storage_handler.execute("INSERT INTO #{DB_TABLE} (key, value, timestamp) VALUES(?, ?, ?)",Marshal.dump(key), Marshal.dump(value), time_entry)
23
+ EH::retry!(:args => [serialize(key), serialize(value), time_entry]) do
24
+ @storage_handler.execute("INSERT INTO #{DB_TABLE} (key, value, timestamp) VALUES(?, ?, ?)",serialize(key), serialize(value), time_entry)
24
25
  end
25
26
  end
26
27
 
27
28
  def lookup_key(key)
28
- EH::retry!(:args => [Marshal.dump(key)]) do
29
- result = @storage_handler.execute("SELECT value, timestamp FROM #{DB_TABLE} WHERE key=?", Marshal.dump(key))
30
- !result.nil? && !result.empty? ? [Marshal.load(result[0][0]), result[0][1]] : nil
29
+ EH::retry!(:args => [serialize(key)]) do
30
+ result = @storage_handler.execute("SELECT value, timestamp FROM #{DB_TABLE} WHERE key=?", serialize(key))
31
+ !result.nil? && !result.empty? ? [deserialize(result[0][0]), result[0][1]] : nil
31
32
  end
32
33
  end
33
34
 
34
35
  def delete_entry(key)
35
- EH::retry!(:args => [Marshal.dump(key)]) do
36
- @storage_handler.execute("DELETE FROM #{DB_TABLE} WHERE key=?", Marshal.dump(key))
36
+ EH::retry!(:args => [serialize(key)]) do
37
+ @storage_handler.execute("DELETE FROM #{DB_TABLE} WHERE key=?", serialize(key))
37
38
  end
38
39
  end
39
40
 
@@ -45,7 +46,7 @@ module Persistent
45
46
 
46
47
  def keys
47
48
  EH::retry!(:args => []) do
48
- @storage_handler.execute("SELECT key FROM #{DB_TABLE}").collect { |k| Marshal.load(k[0]) }
49
+ @storage_handler.execute("SELECT key FROM #{DB_TABLE}").collect { |k| deserialize(k[0]) }
49
50
  end
50
51
  end
51
52
 
@@ -57,6 +58,17 @@ module Persistent
57
58
 
58
59
  private
59
60
 
61
+ def serialize(data)
62
+ Base64.encode64(Marshal.dump(data))
63
+ end
64
+
65
+ def deserialize(data)
66
+ Marshal.load(Base64.decode64(data))
67
+
68
+ rescue TypeError => ex
69
+ Marshal.load(data)
70
+ end
71
+
60
72
  def connect_to_database
61
73
  File.exists?(@storage_details) ? open_database : create_database
62
74
  end
@@ -1,5 +1,5 @@
1
1
  module Persistent
2
2
  class Cache
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'rspec/mocks'
3
3
  require 'tempfile'
4
4
  require 'simplecov'
5
5
  require 'simplecov-rcov'
6
+ require 'debugger'
6
7
 
7
8
  # This file was generated by the `rspec --init` command. Conventionally, all
8
9
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
@@ -10,6 +10,14 @@ describe Persistent::StorageSQLite do
10
10
  @iut = Persistent::StorageSQLite.new(@db_name)
11
11
  end
12
12
 
13
+ def serialize(data)
14
+ Base64.encode64(Marshal.dump(data))
15
+ end
16
+
17
+ def deserialize(data)
18
+ Marshal.load(Base64.decode64(data))
19
+ end
20
+
13
21
  context "when constructed" do
14
22
  it "should create the database if it does not exist" do
15
23
  expect(File.exists?(@db_name)).to eq(true)
@@ -63,10 +71,10 @@ describe Persistent::StorageSQLite do
63
71
  start_time = Time.now - 1
64
72
  @iut.save_key_value_pair(@test_key, @test_value)
65
73
  handle = SQLite3::Database.open(@db_name)
66
- result = handle.execute "select value, timestamp from #{Persistent::StorageSQLite::DB_TABLE} where key=?", Marshal.dump(@test_key)
74
+ result = handle.execute "select value, timestamp from #{Persistent::StorageSQLite::DB_TABLE} where key=?", serialize(@test_key)
67
75
  expect(result.nil?).to eq(false)
68
76
  expect(result[0].nil?).to eq(false)
69
- expect(result[0][0]).to eq(Marshal.dump(@test_value))
77
+ expect(result[0][0]).to eq(serialize(@test_value))
70
78
  test_time = Time.parse(result[0][1])
71
79
  expect(test_time).to be > start_time
72
80
  expect(test_time).to be < start_time + 600
@@ -76,10 +84,10 @@ describe Persistent::StorageSQLite do
76
84
  test_time = (Time.now - 2500)
77
85
  @iut.save_key_value_pair(@test_key, @test_value, test_time)
78
86
  handle = SQLite3::Database.open(@db_name)
79
- result = handle.execute "select value, timestamp from #{Persistent::StorageSQLite::DB_TABLE} where key=?", Marshal.dump(@test_key)
87
+ result = handle.execute "select value, timestamp from #{Persistent::StorageSQLite::DB_TABLE} where key=?", serialize(@test_key)
80
88
  expect(result.nil?).to eq(false)
81
89
  expect(result[0].nil?).to eq(false)
82
- expect(result[0][0]).to eq(Marshal.dump(@test_value))
90
+ expect(result[0][0]).to eq(serialize(@test_value))
83
91
  time_retrieved = Time.parse(result[0][1])
84
92
  expect(time_retrieved.to_s).to eq(test_time.to_s)
85
93
  end
@@ -88,11 +96,11 @@ describe Persistent::StorageSQLite do
88
96
  @iut.save_key_value_pair(@test_key, @test_value)
89
97
  @iut.save_key_value_pair(@test_key, "testvalue2")
90
98
  handle = SQLite3::Database.open(@db_name)
91
- result = handle.execute "select value from #{Persistent::StorageSQLite::DB_TABLE} where key=?", Marshal.dump(@test_key)
99
+ result = handle.execute "select value from #{Persistent::StorageSQLite::DB_TABLE} where key=?", serialize(@test_key)
92
100
  expect(result.nil?).to eq(false)
93
101
  expect(result[0].nil?).to eq(false)
94
102
  expect(result.size).to eq(1)
95
- expect(result[0][0]).to eq(Marshal.dump("testvalue2"))
103
+ expect(result[0][0]).to eq(serialize("testvalue2"))
96
104
  end
97
105
  end
98
106
 
@@ -120,7 +128,7 @@ describe Persistent::StorageSQLite do
120
128
 
121
129
  context "when asked to delete an entry" do
122
130
  it "should not raise an error if the entry is not present" do
123
- @iut.delete_entry(Marshal.dump("shouldnotbepresent"))
131
+ @iut.delete_entry(serialize("shouldnotbepresent"))
124
132
  end
125
133
 
126
134
  it "should delete the entry if it is present" do
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.2.3
4
+ version: 0.3.0
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-05-15 00:00:00.000000000 Z
13
+ date: 2015-05-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec