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) ⇒ CachingConnection

Returns a new instance of CachingConnection.



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

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

Instance Method Details

#cacheObject

Enable the cache within the block.



33
34
35
36
37
38
39
# 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



57
58
59
# File 'lib/active_fedora/caching_connection.rb', line 57

def clear_cache
  @cache.clear
end

#disable_cache!Object



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

def disable_cache!
  @cache_enabled = false
end

#enable_cache!Object



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

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
    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.



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

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