mirror of
https://github.com/valitydev/cache.git
synced 2024-11-06 01:45:19 +00:00
bucket type
This commit is contained in:
parent
84ceff4b3a
commit
f76a6e2276
@ -14,7 +14,6 @@
|
||||
{operations, [
|
||||
{put, 5}
|
||||
,{get, 5}
|
||||
% ,{select, 1}
|
||||
]}.
|
||||
|
||||
%{cache, {mycache, 'cache@127.0.0.1'}}.
|
||||
|
@ -51,8 +51,6 @@
|
||||
remove_/2,
|
||||
acc/3,
|
||||
acc_/3,
|
||||
match/2,
|
||||
fold/4,
|
||||
% memecached like interface
|
||||
set/3,
|
||||
set/4,
|
||||
@ -240,70 +238,6 @@ acc(Cache, Key, Val) ->
|
||||
acc_(Cache, Key, Val) ->
|
||||
gen_server:cast(Cache, {acc, Key, Val}).
|
||||
|
||||
%%
|
||||
%% query cache segments using match specification
|
||||
-spec(match/2 :: (cache(), any()) -> {[any()], any()}).
|
||||
|
||||
match(Cache, {q, [Head|Tail], Req}) ->
|
||||
%% compiled query
|
||||
try
|
||||
case cache_match(Cache, Head, Req, ?CONFIG_SELECT) of
|
||||
'$end_of_table' ->
|
||||
match(Cache, {q, Tail, Req});
|
||||
{Result, Query} ->
|
||||
{Result, {q, Tail, Req, Query}}
|
||||
end
|
||||
catch _:badarg ->
|
||||
%% cache segment was evicted
|
||||
match(Cache, {q, Tail, Req})
|
||||
end;
|
||||
|
||||
match(_Cache, {q, [], _}=Req) ->
|
||||
{eof, Req};
|
||||
|
||||
match(Cache, {q, Heap, Req, '$end_of_table'}) ->
|
||||
match(Cache, {q, Heap, Req});
|
||||
|
||||
match(Cache, {q, Heap, Req, Query0}) ->
|
||||
try
|
||||
case cache_match(Cache, Query0) of
|
||||
'$end_of_table' ->
|
||||
match(Cache, {q, Heap, Req});
|
||||
{Result, Query} ->
|
||||
{Result, {q, Heap, Req, Query}}
|
||||
end
|
||||
catch _:badarg ->
|
||||
%% cache segment was evicted
|
||||
match(Cache, {q, Heap, Req})
|
||||
end;
|
||||
|
||||
match(Cache, Req) ->
|
||||
match(Cache, {q, lists:reverse(i(Cache, heap)), Req}).
|
||||
|
||||
cache_match({_, Node}, Heap, Req, Limit) ->
|
||||
rpc:call(Node, ets, match_object, [Heap, Req, ?CONFIG_SELECT], 60000);
|
||||
|
||||
cache_match(_, Heap, Req, Limit) ->
|
||||
ets:match_object(Heap, Req, Limit).
|
||||
|
||||
cache_match({_, Node}, Req) ->
|
||||
rpc:call(Node, ets, match_object, [Req]);
|
||||
|
||||
cache_match(_, Req) ->
|
||||
ets:match_object(Req).
|
||||
|
||||
%%
|
||||
%% query cache segments and fold function
|
||||
-spec(fold/4 :: (function(), any(), function(), cache()) -> any()).
|
||||
|
||||
fold(Fun, Acc, Req, Cache) ->
|
||||
case match(Cache, Req) of
|
||||
{eof, _} ->
|
||||
Acc;
|
||||
{List, Query} ->
|
||||
fold(Fun, lists:foldl(Fun, Acc, List), Query, Cache)
|
||||
end.
|
||||
|
||||
%%%----------------------------------------------------------------------------
|
||||
%%%
|
||||
%%% memcached-like interface
|
||||
|
@ -58,19 +58,7 @@ run(remove, KeyGen, _ValGen, Cache) ->
|
||||
case (catch cache:remove(cache, Key)) of
|
||||
ok -> {ok, Cache};
|
||||
E -> {error, failure(r, Key, E), Cache}
|
||||
end;
|
||||
|
||||
run(select, KeyGen, _ValGen, Cache) ->
|
||||
Key0 = KeyGen(),
|
||||
Key1 = KeyGen(),
|
||||
cache:fold(
|
||||
fun(_, Acc) -> Acc + 1 end,
|
||||
0,
|
||||
ets:fun2ms(fun({Key, Val}) when Key > Key0, Key < Key1 -> Key end),
|
||||
cache
|
||||
),
|
||||
{ok, Cache}.
|
||||
|
||||
end.
|
||||
|
||||
%%
|
||||
local_init() ->
|
||||
|
Loading…
Reference in New Issue
Block a user