Skip to content

Commit

Permalink
Drop StringKeyCache mutex
Browse files Browse the repository at this point in the history
The StringKeyCache is now only used inside a connection. It's assumed that connections are not used concurrently with multiple queries.
  • Loading branch information
bcardiff committed Dec 2, 2023
1 parent aea76a6 commit 7a19a5d
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/db/string_key_cache.cr
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
module DB
class StringKeyCache(T)
@cache = {} of String => T
@mutex = Mutex.new

def fetch(key : String) : T
@mutex.synchronize do
value = @cache.fetch(key, nil)
value = @cache[key] = yield unless value
value
end
value = @cache.fetch(key, nil)
value = @cache[key] = yield unless value
value
end

def each_value
@mutex.synchronize do
@cache.each do |_, value|
yield value
end
@cache.each do |_, value|
yield value
end
end

def clear
@mutex.synchronize do
@cache.clear
end
@cache.clear
end
end
end

0 comments on commit 7a19a5d

Please sign in to comment.