Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spec/cache_hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ describe CacheHash do
it "returns a time if the kv pair is not stale" do
hash = CacheHash(String).new(Time::Span.new(0, 0, 3))

t = Time.now
t = Time.utc
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
(city_1_time > t && city_1_time < Time.utc).should be_true
end

it "delete the kv pair if it is stale" do
Expand Down
14 changes: 7 additions & 7 deletions src/cache_hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CacheHash(V)

def get(key : String)
if cached_time = @time_hash[key]?
if cached_time > Time.now - @cache_time_span
if cached_time > Time.utc - @cache_time_span
@kv_hash[key]
else
delete key
Expand All @@ -19,7 +19,7 @@ class CacheHash(V)
if val.nil?
delete key
else
@time_hash[key] = Time.now
@time_hash[key] = Time.utc
@kv_hash[key] = val
end
end
Expand All @@ -33,7 +33,7 @@ class CacheHash(V)
def purge_stale
@kv_hash.each_key do |key|
if cached_time = @time_hash[key]?
delete key unless cached_time > Time.now - @cache_time_span
delete key unless cached_time > Time.utc - @cache_time_span
end
end
end
Expand All @@ -51,7 +51,7 @@ class CacheHash(V)

def fresh?(key : String)
if cached_time = @time_hash[key]?
if cached_time > Time.now - @cache_time_span
if cached_time > Time.utc - @cache_time_span
true
else
delete key
Expand All @@ -64,7 +64,7 @@ class CacheHash(V)

def time(key : String)
if cached_time = @time_hash[key]?
if cached_time > Time.now - @cache_time_span
if cached_time > Time.utc - @cache_time_span
cached_time
else
delete key
Expand All @@ -74,8 +74,8 @@ class CacheHash(V)

def refresh(key : String)
if cached_time = @time_hash[key]?
if cached_time > Time.now - @cache_time_span
@time_hash[key] = Time.now
if cached_time > Time.utc - @cache_time_span
@time_hash[key] = Time.utc
@kv_hash[key]
else
delete key
Expand Down