-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix concurrency issues with file caches
Fixes #300
- Loading branch information
Showing
6 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'i18n/tasks/concurrent/cached_value' | ||
|
||
module I18n::Tasks::Concurrent | ||
# A thread-safe cache. | ||
# @since 0.9.25 | ||
class Cache | ||
def initialize | ||
@mutex = Mutex.new | ||
@map = {} | ||
end | ||
|
||
# @param [Object] key | ||
# @return [Object] Cached or computed value. | ||
def fetch(key, &block) | ||
@mutex.synchronize do | ||
@map[key] ||= CachedValue.new(&block) | ||
end.get | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'i18n/tasks/concurrent/cached_value' | ||
|
||
module I18n::Tasks::Concurrent | ||
# A thread-safe memoized value. | ||
# The given computation is guaranteed to be invoked at most once. | ||
# @since 0.9.25 | ||
class CachedValue | ||
NULL = Object.new | ||
|
||
# @param [Proc] computation The computation that returns the value to cache. | ||
def initialize(&computation) | ||
@computation = computation | ||
@mutex = Mutex.new | ||
@result = NULL | ||
end | ||
|
||
# @return [Object] Result of the computation. | ||
def get | ||
return @result unless @result == NULL | ||
@mutex.synchronize do | ||
@result = @computation.call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters