Class: ActiveFedora::CachingConnection

Inherits:
Ldp::Client
  • Object
show all
Defined in:
lib/active_fedora/caching_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ CachingConnection

Returns a new instance of CachingConnection.



3
4
5
6
7
# File 'lib/active_fedora/caching_connection.rb', line 3

def initialize(host, options = {})
  super
  @cache = {}
  @cache_enabled = false
end

Instance Method Details

#cacheObject

Enable the cache within the block.



33
34
35
36
37
38
39
40
# File 'lib/active_fedora/caching_connection.rb', line 33

def cache
  old = @cache_enabled
  @cache_enabled = true
  yield
ensure
  @cache_enabled = old
  clear_cache unless @cache_enabled
end

#clear_cacheObject



59
60
61
# File 'lib/active_fedora/caching_connection.rb', line 59

def clear_cache
  @cache.clear
end

#disable_cache!Object



46
47
48
# File 'lib/active_fedora/caching_connection.rb', line 46

def disable_cache!
  @cache_enabled = false
end

#enable_cache!Object



42
43
44
# File 'lib/active_fedora/caching_connection.rb', line 42

def enable_cache!
  @cache_enabled = true
end

#get(url, options = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/active_fedora/caching_connection.rb', line 9

def get(url, options = {})
  if @cache_enabled
    cache_resource(url) { super }
  else
    log(url) { super }
  end
end

#patchObject



27
28
29
30
# File 'lib/active_fedora/caching_connection.rb', line 27

def patch(*)
  clear_cache if @cache_enabled
  super
end

#postObject



17
18
19
20
# File 'lib/active_fedora/caching_connection.rb', line 17

def post(*)
  clear_cache if @cache_enabled
  super
end

#putObject



22
23
24
25
# File 'lib/active_fedora/caching_connection.rb', line 22

def put(*)
  clear_cache if @cache_enabled
  super
end

#uncachedObject

Disable the query cache within the block.



51
52
53
54
55
56
57
# File 'lib/active_fedora/caching_connection.rb', line 51

def uncached
  old = @cache_enabled
  @cache_enabled = false
  yield
ensure
  @cache_enabled = old
end