This cookbook provides a resource_cache
LWRP. Its purpose is to save time
for cookbook authors. It can cache expensive and time-consuming resource
invocations between chef runs.
Resource caching is disabled by default. The goal is to make the LWRP easy and safe to use for cookbook authors without surprising end-users.
None -- just Chef.
0.5
###Quick Start
- Wrap resources in
resource_cache
blocks - Set
resource_cache['enable'] = true
- Set
resource_cache['ttl'] = <minutes>
- Cookbook authors: please do NOT set these attributes as part of a library cookbook you distribute. Instead, set them locally for your development cycle.
###Simple caching: # Cache until TTL expiration resource_cache do resources do execute 'apt-get update' end end
###Conditional caching: # Cache until TTL expiration or conditions change resource_cache do conditions ({ :prefix => '/opt/software', :version => '1.0' }) resources do bash 'compile software' do # Expensive compilation... end bash 'install software' do # Expensive copying/installing end end end
Note conditions ({})
above. Using conditions {}
would be interpreted as
a block instead of a hash.
The LWRP serializes state to json files under chef's cache directory. It stores a timestamp and the cache conditions (if specified) when a wrapped resource is updated. On subsequent chef runs, the serialized state is loaded. If the current cache conditions match the prior cache conditions and the updated timestamp is within the TTL, the wrapped resources are skipped.
Any of the following will disable cache entries:
- Setting
bypass true
for a cache entry - Changing the
conditions ({})
for a cache entry - Setting the resource_cache.ttl attribute to 0
- Setting the resource_cache.enable attribute to false
- Removing the cache files from the resource_cache.dir directory
Key | Type | Description | Default |
---|---|---|---|
['resource_cache']['enable'] | Boolean | whether to enable the cache | false |
['resource_cache']['ttl'] | Integer | Time-to-live (expiration) in minutes | 0 |
['resource_cache']['dir'] | String (path) | Directory to store cache files | Chef::Config[:file_cache_path]/resource_cache, or [SYSTEM TMP]/resource_cache if :file_cache_path is unset |
If you use vagrant for Chef development (highly recommended), watch out for conflict with vagrant cache plugins. Some of these may store /var/chef/cache outside of the VM, on the host filesystem. If the contents of this directory are retained after a VM is destroyed, the resource_cache might produce false cache hits. The solution is to set the resource_cache.dir attribute to a directory that is kept within the VM (e.g. /var/chef-resource-cache).
- Write tests. Yes, I'm a horrible person for releasing this without tests...
- Normalize cache file names by stripping/replacing characters that aren't shell friendly.
- Fork the repository on Github
- Create a named feature branch (like
add_tests
-- wink wink) - Write your change
- Submit a Pull Request using Github
Copyright 2013, Bob Ziuchkovski [email protected]
Copyright 2013, UserTesting.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and