Class: MemoryInvalidationMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/macaw_framework/middlewares/memory_invalidation_middleware.rb

Overview

Middleware responsible for storing and invalidating cache.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inv_time_seconds = 3_600) ⇒ MemoryInvalidationMiddleware

Returns a new instance of MemoryInvalidationMiddleware.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/macaw_framework/middlewares/memory_invalidation_middleware.rb', line 9

def initialize(inv_time_seconds = 3_600)
  @cache = {}
  @mutex = Mutex.new
  Thread.new do
    loop do
      sleep(1)
      @mutex.synchronize do
        @cache.each_pair do |key, value|
          @cache.delete(key) if Time.now - value[1] >= inv_time_seconds
        end
      end
    end
  end
  sleep(2)
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/macaw_framework/middlewares/memory_invalidation_middleware.rb', line 7

def cache
  @cache
end

#mutexObject

Returns the value of attribute mutex.



7
8
9
# File 'lib/macaw_framework/middlewares/memory_invalidation_middleware.rb', line 7

def mutex
  @mutex
end