Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a procedure for invalidating the cache. #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ erased, but the memory storing `mf(4)` is still in use.

The second parameter also allows sharing a single cache amongst two memoized functions.

Optionally, the second parameter may contain a validator method for determining
the validity of a cache entry. When the validator returns nil or false,
the current cache entry is discarded and a new one will be computed.
For example, suppose that we want to declare a cache entry stale after 10
seconds.

``` lua
local cache = {}
function cache:validator(current_validity)
if current_validity == nil then
return os.time()
elseif (os.time() - current_validity) > 10 then
return nil
end
return current_validity
end
```

## Changelog

Expand Down
9 changes: 9 additions & 0 deletions memoize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ local function cache_get(cache, params)
node = node.children and node.children[params[i]]
if not node then return nil end
end
if node.results and cache.validator then
node.validity = cache:validator(node.validity)
if not node.validity then
node.results = nil
end
end
return node.results
end

Expand All @@ -63,6 +69,9 @@ local function cache_put(cache, params, results)
node = node.children[param]
end
node.results = results
if cache.validator then
node.validity = cache:validator(nil)
end
end

-- public function
Expand Down
29 changes: 29 additions & 0 deletions rockspecs/memoize-scm-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package = "memoize"
version = "scm-0"
source = {
url = "git+https://github.com/kikito/memoize.lua.git"
}
description = {
summary = "Memoized functions in Lua",
detailed = [[
* Caches the results based on multiple parameters instead of just 1.
* Doesn't rely on `tostring`; instead, it uses operator `==` on all the
parameters (this is accomplished by structuring the cache in a
tree structure, where each tree node corresponds to one
parameter).
* Works well with functions returning multiple values
* Can memoize both functions and "callable tables" (tables with a `__call`
metamethod)
]],
homepage = "https://github.com/kikito/memoize.lua",
license = "MIT"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
memoize = "memoize.lua"
}
}
28 changes: 26 additions & 2 deletions spec/memoize_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ describe('memoize', function()
assert.equal(4, mlen('a', 'b', 'c', 'd'))
assert.equal(11, counter)
end)
end)

end)
it("invalidates based on an optional validator", function()
local cache = {}
function cache:validator(current_validity)
if not current_validity then
return 3
elseif current_validity == 0 then
return nil
end
return current_validity - 1
end
local mlen = memoize(len, cache)

assert.equal(1, mlen('a'))
assert.equal(1, counter)
assert.equal(1, mlen('a'))
assert.equal(1, counter)
assert.equal(1, mlen('a'))
assert.equal(1, counter)
assert.equal(1, mlen('a'))
assert.equal(1, counter)
assert.equal(1, mlen('a'))
assert.equal(2, counter)
end)

end)
end)