Skip to content

Commit

Permalink
Merge pull request #1 from samueleaton/fix-value-type
Browse files Browse the repository at this point in the history
Fix where value type was hardcoded as 'String'
  • Loading branch information
samueleaton committed Aug 26, 2017
2 parents fe75262 + a58810d commit 9051430
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
46 changes: 32 additions & 14 deletions spec/cache_hash_spec.cr
Expand Up @@ -2,7 +2,7 @@ require "./spec_helper"

describe CacheHash do
it "saves key value pairs" do
hash = CacheHash(String).new(Time::Span.new(0,0,4))
hash = CacheHash(String).new(Time::Span.new(0, 0, 4))
hash.set "city_1", "Seattle"
hash.set "city_2", "Honk Kong"
hash.set "city_3", "Sacramento"
Expand All @@ -11,8 +11,26 @@ describe CacheHash do
hash.get("city_3").should eq("Sacramento")
end

it "allows different value types" do
hash = CacheHash(Int32).new(Time::Span.new(0, 0, 4))
hash.set "key1", 1111
hash.set "key2", 2222
hash.set "key3", 3333
hash.get("key1").should eq(1111)
hash.get("key2").should eq(2222)
hash.get("key3").should eq(3333)

hash2 = CacheHash(Int32 | String | Bool).new(Time::Span.new(0, 0, 4))
hash2.set "key1", 1111
hash2.set "key2", "two"
hash2.set "key3", false
hash2.get("key1").should eq(1111)
hash2.get("key2").should eq("two")
hash2.get("key3").should eq(false)
end

it "removes stale kv pairs on lookup" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand Down Expand Up @@ -45,7 +63,7 @@ describe CacheHash do

describe "#purge_stale" do
it "removes all stale, expired values from the hash" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand All @@ -72,7 +90,7 @@ describe CacheHash do

describe "#keys" do
it "purges all stale values and returns the IDs of non-stale kv pairs" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand All @@ -83,7 +101,7 @@ describe CacheHash do
sleep 1
hash.set "city_5", "Denver"
sleep 1

hash.raw["city_1"].should eq("Seattle")
hash.raw["city_2"].should eq("Honk Kong")
hash.raw["city_3"].should eq("Sacramento")
Expand All @@ -102,7 +120,7 @@ describe CacheHash do

describe "#fresh?" do
it "returns a true if the kv pair is not stale" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand All @@ -113,7 +131,7 @@ describe CacheHash do
sleep 1
hash.set "city_5", "Denver"
sleep 1

hash.fresh?("city_1").should be_false
hash.fresh?("city_4").should be_true
hash.fresh?("xxxxx").should be_false
Expand All @@ -122,7 +140,7 @@ describe CacheHash do
end

it "removes deletes the kv pair if it is stale" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand All @@ -133,7 +151,7 @@ describe CacheHash do
sleep 1
hash.set "city_5", "Denver"
sleep 1

hash.raw["city_1"].should eq("Seattle")
hash.raw["city_2"].should eq("Honk Kong")
hash.raw["city_3"].should eq("Sacramento")
Expand All @@ -147,18 +165,18 @@ describe CacheHash do

describe "#time" do
it "returns a time if the kv pair is not stale" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))

t = Time.now
hash.set "city_1", "Seattle"
hash.time("city_1").class.should eq(Time)

city_1_time = hash.time("city_1").not_nil!
(city_1_time > t && city_1_time < Time.now).should be_true
end

it "delete the kv pair if it is stale" do
hash = CacheHash(String).new(Time::Span.new(0,0,3))
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))
hash.set "city_1", "Seattle"
sleep 1
hash.set "city_2", "Honk Kong"
Expand All @@ -169,7 +187,7 @@ describe CacheHash do
sleep 1
hash.set "city_5", "Denver"
sleep 1

hash.raw["city_1"].should eq("Seattle")
hash.raw["city_2"].should eq("Honk Kong")
hash.raw["city_3"].should eq("Sacramento")
Expand Down
8 changes: 4 additions & 4 deletions src/cache_hash.cr
@@ -1,9 +1,9 @@
class CacheHash(V)
def initialize(@cache_time_span : Time::Span)
@kv_hash = {} of String => String
@kv_hash = {} of String => V
@time_hash = {} of String => Time
end

def get(key : String)
if cached_time = @time_hash[key]?
if cached_time > Time.now - @cache_time_span
Expand All @@ -14,7 +14,7 @@ class CacheHash(V)
end
end
end

def set(key : String, val : V | Nil)
if val.nil?
delete key
Expand All @@ -30,7 +30,7 @@ class CacheHash(V)
nil
end

def purge_stale()
def purge_stale
@kv_hash.select! do |k, v|
if cached_time = @time_hash[k]?
cached_time > Time.now - @cache_time_span
Expand Down

0 comments on commit 9051430

Please sign in to comment.