Class: HTTPX::Plugins::ResponseCache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/response_cache/store.rb

Overview

Implementation of a thread-safe in-memory cache store.

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



7
8
9
10
# File 'lib/httpx/plugins/response_cache/store.rb', line 7

def initialize
  @store = {}
  @store_mutex = Thread::Mutex.new
end

Instance Method Details

#clearObject



12
13
14
# File 'lib/httpx/plugins/response_cache/store.rb', line 12

def clear
  @store_mutex.synchronize { @store.clear }
end

#get(request) ⇒ Object



16
17
18
19
20
# File 'lib/httpx/plugins/response_cache/store.rb', line 16

def get(request)
  @store_mutex.synchronize do
    @store[request.response_cache_key]
  end
end

#set(request, response) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/httpx/plugins/response_cache/store.rb', line 22

def set(request, response)
  @store_mutex.synchronize do
    cached_response = @store[request.response_cache_key]

    cached_response.close if cached_response

    @store[request.response_cache_key] = response
  end
end