Class: ElFinderS3::CacheConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/el_finder_s3/cache_connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ CacheConnector

Returns a new instance of CacheConnector.



5
6
7
# File 'lib/el_finder_s3/cache_connector.rb', line 5

def initialize(client = nil)
  @cache = client.nil? ? ElFinderS3::DummyCacheClient.new : Cache.wrap(client)
end

Instance Method Details

#cache_hash(operation, pathname) ⇒ Object



9
10
11
# File 'lib/el_finder_s3/cache_connector.rb', line 9

def cache_hash(operation, pathname)
  Base64.urlsafe_encode64("#{operation}::#{pathname}").chomp.tr("=\n", "")
end

#cached(operation, pathname) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/el_finder_s3/cache_connector.rb', line 13

def cached(operation, pathname)
  cache_hash_key = cache_hash(operation, pathname)
  if @cache.exist? cache_hash_key
    return @cache.get(cache_hash_key)
  else
    response = yield

    @cache.set(cache_hash_key, response, 2.years)

    response
  end
end

#clear_cache(pathname, recursive = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/el_finder_s3/cache_connector.rb', line 26

def clear_cache(pathname, recursive = true)
  ElFinderS3::Operations.each do |operation|
    @cache.delete cache_hash(operation, pathname)
  end

  if recursive || pathname.file?
    pathname_str = pathname.to_s
    if pathname_str != '/' && pathname_str != '.'
      clear_cache(pathname.dirname, recursive)
    end
  end
end