forked from spilgames/erl-cache
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Propagate cache stats from ets operations and fix dialyzer issues #23
Open
matyasmarkovics
wants to merge
6
commits into
alertlogic:master
Choose a base branch
from
matyasmarkovics:matyas-refactor-operate-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54be9fb
Define cache-operation function type, use arguments known at compile-…
matyasmarkovics d9c8e7d
Fix no-local-return of decorator
matyasmarkovics 1deeb7a
Fix incorrect type-descriptions
matyasmarkovics 4711f6f
Ignore test artifacts
matyasmarkovics cb6b297
Do not leak Options usage into decorator module
matyasmarkovics 877cdfe
Fix unknown-type dialyzer error
matyasmarkovics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ doc/doc | |
tests/ | ||
/erl_cache | ||
.rebar3 | ||
TEST-*.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
-module(erl_cache_decorator). | ||
-behaviour(erl_cache_key_generator). | ||
|
||
-export([cache_pt/3]). | ||
-export([generate_key/4]). | ||
|
||
%% ==================================================================== | ||
%% API | ||
%% ==================================================================== | ||
|
||
-spec cache_pt(function(), [term()], {atom(), atom(), erl_cache:name(), erl_cache:cache_opts()}) -> | ||
(fun(() -> term())). | ||
-spec cache_pt(Fun, Args, Decoration) -> Arity0Fun when | ||
Fun::function(), | ||
Args::[term()], | ||
Decoration::{Module::module(), FunctionAtom::atom(), Name, Opts}, | ||
Name::erl_cache:name(), | ||
Opts::erl_cache:cache_opts(), | ||
Arity0Fun::fun(() -> term()). | ||
cache_pt(Fun, Args, {Module, FunctionAtom, Name, Opts}) -> | ||
FinalOpts = [{refresh_callback, fun () -> Fun(Args) end} | Opts], | ||
Key = case proplists:get_value(key_generation, Opts) of | ||
KeyModule when is_atom(KeyModule), KeyModule /= undefined -> | ||
apply(KeyModule, generate_key, [Name, Module, FunctionAtom, Args]); | ||
_ -> | ||
{decorated, Module, FunctionAtom, crypto:hash(sha, erlang:term_to_binary(Args))} | ||
end, | ||
FromCache = erl_cache:get(Name, Key, FinalOpts), | ||
case FromCache of | ||
{ok, Result} -> fun() -> Result end; | ||
KeyModule = key_module(Opts), | ||
Key = KeyModule:generate_key(Name, Module, FunctionAtom, Args), | ||
case erl_cache:get(Name, Key, Opts) of | ||
{ok, Result} -> | ||
fun() -> Result end; | ||
{error, not_found} -> | ||
fun () -> | ||
Res = Fun(Args), | ||
ok = erl_cache:set(Name, Key, Res, FinalOpts), | ||
Res | ||
end; | ||
cache_setter(Name, Key, Opts, Fun, Args); | ||
{error, Err} -> | ||
throw({error, {cache_pt, Err}}) | ||
end. | ||
|
||
%% ==================================================================== | ||
%% Behaviour callback | ||
%% ==================================================================== | ||
generate_key(_Name, Module, FunctionAtom, Args) -> | ||
{decorated, Module, FunctionAtom, crypto:hash(sha, erlang:term_to_binary(Args))}. | ||
|
||
%% ==================================================================== | ||
%% Internal | ||
%% ==================================================================== | ||
key_module(Opts) -> | ||
case proplists:get_value(key_generation, Opts) of | ||
undefined -> ?MODULE; | ||
KeyMod when is_atom(KeyMod) -> KeyMod; | ||
_ -> ?MODULE | ||
end. | ||
|
||
cache_setter(Name, Key, Opts, Fun, Args) -> | ||
Callback = fun() -> Fun(Args) end, | ||
SetOpts = [{refresh_callback, Callback} | Opts], | ||
fun() -> | ||
Value = Callback(), | ||
ok = erl_cache:set(Name, Key, Value, SetOpts), | ||
Value | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make use of
operate_cache/5
.