Class: Dragonfly::Plugin::Cache::Local
- Inherits:
-
Object
- Object
- Dragonfly::Plugin::Cache::Local
- Defined in:
- lib/dragonfly/plugin/cache/local.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#public_path ⇒ Object
readonly
Returns the value of attribute public_path.
Class Method Summary collapse
Instance Method Summary collapse
- #call(app, options = {}) ⇒ Object
-
#initialize(public_path:, cache_path: 'dragonfly-cache') ⇒ Local
constructor
A new instance of Local.
- #job_cache_path(job) ⇒ Object
- #read_disk_cache ⇒ Object
- #store(job) ⇒ Object
- #url_for(app, job, opts) ⇒ Object
Constructor Details
#initialize(public_path:, cache_path: 'dragonfly-cache') ⇒ Local
Returns a new instance of Local.
18 19 20 21 22 23 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 18 def initialize(public_path:, cache_path: 'dragonfly-cache') @public_path = public_path @cache_path = cache_path FileUtils.mkdir_p(File.join(public_path, cache_path)) @cache = read_disk_cache end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
16 17 18 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 16 def cache @cache end |
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
16 17 18 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 16 def cache_path @cache_path end |
#public_path ⇒ Object (readonly)
Returns the value of attribute public_path.
16 17 18 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 16 def public_path @public_path end |
Class Method Details
.extract_public_path(path:, public_path:) ⇒ Object
11 12 13 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 11 def extract_public_path(path:, public_path:) path[/#{public_path}(.+)/, 1] end |
.extract_sha(path:) ⇒ Object
7 8 9 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 7 def extract_sha(path:) path.to_s.split('/')[-2] end |
Instance Method Details
#call(app, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 44 def call(app, = {}) app.server.before_serve do |job, env| store(job) end app.define_url do |app, job, opts| url_for(app, job, opts) end end |
#job_cache_path(job) ⇒ Object
34 35 36 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 34 def job_cache_path(job) File.join(public_path, cache_path, job.sha, [job.basename, job.ext].join('.')) end |
#read_disk_cache ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 25 def read_disk_cache c = {} Dir[File.join(public_path, cache_path, '*', '*')].each do |path| sha = self.class.extract_sha(path: path) c[sha] = self.class.extract_public_path(path: path, public_path: public_path) end c end |
#store(job) ⇒ Object
38 39 40 41 42 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 38 def store(job) fullpath = job_cache_path(job) @cache[job.sha] = self.class.extract_public_path(path: fullpath, public_path: public_path) job.to_file(fullpath, mode: 0644) unless File.exists?(fullpath) end |
#url_for(app, job, opts) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dragonfly/plugin/cache/local.rb', line 54 def url_for(app, job, opts) sha = job.sha if (path = @cache[sha]) path elsif (fullpath = Dir[File.join(public_path, cache_path, sha, '*')].first) @cache[sha] = self.class.extract_public_path(path: fullpath, public_path: public_path) else app.server.url_for(job, opts) end end |