Class: DerivedImages::Cache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/derived_images/cache.rb

Overview

Build cache for derived image files.

Instance Method Summary collapse

Constructor Details

#initialize(path = DerivedImages.config.cache_path) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • path (Pathname, String, nil) (defaults to: DerivedImages.config.cache_path)

    The cache directory, which will be created if missing. Nil disables caching.



9
10
11
# File 'lib/derived_images/cache.rb', line 9

def initialize(path = DerivedImages.config.cache_path)
  @path = path && Pathname.new(path)
end

Instance Method Details

#copy(key, path) ⇒ Object

Copy a cached file out to another path.

Parameters:

  • key (String)

    Cache key

  • path (Pathname, String)


17
18
19
# File 'lib/derived_images/cache.rb', line 17

def copy(key, path)
  enabled? && FileUtils.copy(key_path(key), path)
end

#digest(key) ⇒ String?

Get the SHA256 hash of a cached file.

Parameters:

  • key (String)

    Cache key

Returns:

  • (String, nil)

    The hex-encoded digest



84
85
86
# File 'lib/derived_images/cache.rb', line 84

def digest(key)
  Digest::SHA256.file(key_path(key)).hexdigest if exist?(key)
end

#each {|key| ... } ⇒ DerivedImages::Cache, Enumerator

Iterates over cache keys.

Yield Parameters:

  • key (String)

    the cache key

Returns:



73
74
75
76
77
78
# File 'lib/derived_images/cache.rb', line 73

def each
  return enum_for(:each) unless block_given?

  path.glob('*/*') { |path| yield path.to_s.last(65).delete('/') } if enabled?
  self
end

#exist?(key) ⇒ Boolean

Check for the presence of a cached file.

Parameters:

  • key (String)

    Cache key

Returns:

  • (Boolean)


25
26
27
# File 'lib/derived_images/cache.rb', line 25

def exist?(key)
  enabled? && key_path(key).file?
end

#key_path(key) ⇒ Pathname

Convert a cache key into a filesystem path of where it would be stored.

Parameters:

  • key (String)

    Cache key

Returns:

  • (Pathname)


65
66
67
# File 'lib/derived_images/cache.rb', line 65

def key_path(key)
  path.join(key[0...2], key[2..])
end

#remove(key) ⇒ Object

Remove a cached file, if it exists.

Parameters:

  • key (String)

    Cache key



32
33
34
35
36
37
# File 'lib/derived_images/cache.rb', line 32

def remove(key)
  return unless enabled?

  key_path(key).delete if key_path(key).exist?
  maybe_clean_dir_for(key)
end

#store(key, path) ⇒ Object

Copy a file into the cache.

Parameters:

  • key (String)

    Cache key

  • path (Pathname, String)


43
44
45
46
47
48
# File 'lib/derived_images/cache.rb', line 43

def store(key, path)
  return unless enabled?

  mkdir_for(key)
  FileUtils.copy(path, key_path(key))
end

#take_and_store(key, path) ⇒ Object

Move (not copy) a file into the cache.

Parameters:

  • key (String)

    Cache key

  • path (Pathname, String)


54
55
56
57
58
59
# File 'lib/derived_images/cache.rb', line 54

def take_and_store(key, path)
  return unless enabled?

  mkdir_for(key)
  FileUtils.mv(path, key_path(key))
end