$ luarocks install cycle-cache# cycle-cache
A cycle-based cache for Lua.
The type maintains a cycle-count of caches, and on fetching, refreshes the
newest cache with the value if it matches. When you cycle the cache, it
rotates the caches, dropping the oldest.
## API
```lua
local CycleCache = require 'cycle-cache'
```
### `CycleCache.new(cycles?)`
Creates a new cache that will hold `cycles` cycles (defaulting to 2).
If `cycles` is less than 1, this will not cache at all.
If `cycles` is 1, then each cycle will completely empty the cache.
### `CycleCache.gc(cycles?)`
Creates a new cache that automatically cycles with each garbage collection cycle.
This does not work in Lua 5.1.
### `CycleCache:raw_get(key)`
Gets the value in the cache, or `nil` if there is none in the cache.
A found value **does not** update the cache for this method.
### `CycleCache:get(key, factory?)`
Gets the value in the cache, or `nil` if there is none in the cache. If the
value is found in an older cache cycle, it is refreshed.
If there is a factory, an absent value is automatically created and inserted
into the cache, and returned instead.
### `CycleCache:set(key, value)`
Sets the value in the cache and returns it.
If `value` is nil, clears the key from all cycles of the cache.
### `CycleCache:cycle()`
Cycle the cache, dropping all entries older than the cycle maximum.