Class: AsyncCache
- Inherits:
-
Object
- Object
- AsyncCache
- Includes:
- Singleton
- Defined in:
- app/models/async_cache.rb
Instance Method Summary collapse
- #build ⇒ Object
- #counter ⇒ Object
-
#initialize ⇒ AsyncCache
constructor
A new instance of AsyncCache.
-
#read(tag) ⇒ Object
since the async controller read request happens immediately on pages that use the ‘asynchronously do` block, the AsyncCache#read method has to block until the file it’s looking for exists.
- #schedule(&block) ⇒ Object
- #write(tag, content) ⇒ Object
Constructor Details
#initialize ⇒ AsyncCache
Returns a new instance of AsyncCache.
4 5 6 7 |
# File 'app/models/async_cache.rb', line 4 def initialize @@counter = 0 @@chunks = [] end |
Instance Method Details
#build ⇒ Object
18 19 20 |
# File 'app/models/async_cache.rb', line 18 def build @@chunks.each {|chunk| chunk.call } end |
#counter ⇒ Object
14 15 16 |
# File 'app/models/async_cache.rb', line 14 def counter @@counter end |
#read(tag) ⇒ Object
since the async controller read request happens immediately on pages that use the ‘asynchronously do` block, the AsyncCache#read method has to block until the file it’s looking for exists.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/async_cache.rb', line 25 def read(tag) @content = nil begin status = Timeout.timeout(10) do until File.exists?(path(tag)) sleep 0.2 end @content = File.read(path(tag)) end rescue Timeout::Error Rails.logger.error("\e[31mTimeout Error in AsyncCache\e[0m") @content = "" end @content end |
#schedule(&block) ⇒ Object
9 10 11 12 |
# File 'app/models/async_cache.rb', line 9 def schedule(&block) @@counter += 1 @@chunks << block end |
#write(tag, content) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'app/models/async_cache.rb', line 42 def write(tag, content) if !File.exists?(parent) setup end File.open(path(tag), 'w') do |f| f.write(content) end end |