Skip to content

Commit

Permalink
convert ms to ns correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
snichme committed Jan 17, 2025
1 parent 228838c commit e7ac47d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/lavinmq/deduplication.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ module LavinMQ

class MemoryCache(T) < Cache(T)
def initialize(@size : UInt32)
@store = Hash(T, Int64?).new
@store = Hash(T, Time::Span?).new
end

def contains?(key : T) : Bool
return false unless @store.has_key?(key)
ttd = @store[key]
return true unless ttd
return true if ttd > RoughTime.unix_ms
return true if ttd > RoughTime.monotonic
@store.delete(key)
false
end

def insert(key : T, ttl : UInt32? = nil)
@store.shift if @store.size >= @size
@store[key] = ttl ? RoughTime.unix_ms + ttl : nil
val = if ttl
RoughTime.monotonic + Time::Span.new(nanoseconds: ttl * 1_000_000)
else
nil
end
@store[key] = val
end
end

Expand Down

0 comments on commit e7ac47d

Please sign in to comment.