Sprockets::Caching

`Caching` is an internal mixin whose public methods are exposed on the `Environment` and `Index` classes.

Protected Instance Methods

cache_asset(path) click to toggle source

Cache helper method. Takes a `path` argument which maybe a logical path or fully expanded path. The `&block` is passed for finding and building the asset if its not in cache.

    # File lib/sprockets/caching.rb, line 9
 9:       def cache_asset(path)
10:         # If `cache` is not set, return fast
11:         if cache.nil?
12:           yield
13: 
14:         # Check cache for `path`
15:         elsif (asset = Asset.from_hash(self, cache_get_hash(path.to_s))) && asset.fresh?(self)
16:           asset
17: 
18:          # Otherwise yield block that slowly finds and builds the asset
19:         elsif asset = yield
20:           hash = {}
21:           asset.encode_with(hash)
22: 
23:           # Save the asset to its path
24:           cache_set_hash(path.to_s, hash)
25: 
26:           # Since path maybe a logical or full pathname, save the
27:           # asset its its full path too
28:           if path.to_s != asset.pathname.to_s
29:             cache_set_hash(asset.pathname.to_s, hash)
30:           end
31: 
32:           asset
33:         end
34:       end

Private Instance Methods

cache_get(key) click to toggle source

Low level cache getter for `key`. Checks a number of supported cache interfaces.

    # File lib/sprockets/caching.rb, line 59
59:       def cache_get(key)
60:         # `Cache#get(key)` for Memcache
61:         if cache.respond_to?(:get)
62:           cache.get(key)
63: 
64:         # `Cache#[key]` so `Hash` can be used
65:         elsif cache.respond_to?(:[])
66:           cache[key]
67: 
68:         # `Cache#read(key)` for `ActiveSupport::Cache` support
69:         elsif cache.respond_to?(:read)
70:           cache.read(key)
71: 
72:         else
73:           nil
74:         end
75:       end
cache_get_hash(key) click to toggle source
    # File lib/sprockets/caching.rb, line 44
44:       def cache_get_hash(key)
45:         hash = cache_get(expand_cache_key(key))
46:         if hash.is_a?(Hash) && digest.hexdigest == hash['_version']
47:           hash
48:         end
49:       end
cache_set(key, value) click to toggle source

Low level cache setter for `key`. Checks a number of supported cache interfaces.

    # File lib/sprockets/caching.rb, line 79
79:       def cache_set(key, value)
80:         # `Cache#set(key, value)` for Memcache
81:         if cache.respond_to?(:set)
82:           cache.set(key, value)
83: 
84:         # `Cache#[key]=value` so `Hash` can be used
85:         elsif cache.respond_to?(:[]=)
86:           cache[key] = value
87: 
88:         # `Cache#write(key, value)` for `ActiveSupport::Cache` support
89:         elsif cache.respond_to?(:write)
90:           cache.write(key, value)
91:         end
92: 
93:         value
94:       end
cache_set_hash(key, hash) click to toggle source
    # File lib/sprockets/caching.rb, line 51
51:       def cache_set_hash(key, hash)
52:         hash['_version'] = digest.hexdigest
53:         cache_set(expand_cache_key(key), hash)
54:         hash
55:       end
expand_cache_key(key) click to toggle source

Strips `Environment#root` from key to make the key work consisently across different servers. The key is also hashed so it does not exceed 250 characters.

    # File lib/sprockets/caching.rb, line 40
40:       def expand_cache_key(key)
41:         File.join('sprockets', digest_class.hexdigest(key.sub(root, '')))
42:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.