rabbit_auth_cache behaviour

This commit is contained in:
Daniil Fedotov 2016-05-19 10:18:02 +01:00
parent e2b27f5e15
commit 118a6648ec
7 changed files with 54 additions and 4 deletions

View File

@ -59,7 +59,8 @@ You can configure TTL for cache items, by using `cache_ttl` configuration item,
{cache_ttl, 5000}]}].
You can also use a custom cache module to store cached requests. This module
should be an erlang module implementing `rabbit_auth_cache` behaviour.
should be an erlang module implementing `rabbit_auth_cache` behaviour and (optionally)
define `start_link` function to start cache process.
This repository contains three such modules:
@ -70,6 +71,7 @@ This repository contains three such modules:
To specify module for caching you should use `cache_module` configuration item and
specify start args with `cache_module_args`.
Start args should be list of arguments passed to module `start_link` function
[{rabbitmq_auth_backend_cache, [{cache_module, rabbit_auth_backend_ets_segmented},
{cache_module_args, [10000]}]}].

View File

@ -33,6 +33,12 @@ stop(_State) ->
init([]) ->
{ok, AuthCache} = application:get_env(rabbitmq_auth_backend_cache,
cache_module),
{ok, AuthCacheArgs} = application:get_env(rabbitmq_auth_backend_cache, cache_module_args),
{ok, {{one_for_one,3,10},[{auth_cache, {AuthCache, start_link, AuthCacheArgs},
permanent, 5000, worker, [AuthCache]}]}}.
ChildSpecs = case erlang:function_exported(AuthCache, start_link,
length(AuthCacheArgs)) of
true -> [{auth_cache, {AuthCache, start_link, AuthCacheArgs},
permanent, 5000, worker, [AuthCache]}];
false -> []
end,
{ok, {{one_for_one,3,10}, ChildSpecs}}.

View File

@ -0,0 +1,37 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (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.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
%%
-module(rabbit_auth_cache).
-ifdef(use_specs).
-callback get(term()) -> term().
-callback put(term(), term(), integer()) -> ok.
-callback delete(term()) -> ok.
-else.
-export([behaviour_info/1]).
behaviour_info(callbacks) ->
[{get, 1}, {put, 3}, {delete, 1}];
behaviour_info(_Other) ->
undefined.
-endif.

View File

@ -19,6 +19,8 @@
-compile({no_auto_import,[get/1]}).
-compile({no_auto_import,[put/2]}).
-behaviour(rabbit_auth_cache).
-export([start_link/0,
get/1, put/3, delete/1]).

View File

@ -18,7 +18,8 @@
-behaviour(gen_server2).
-compile({no_auto_import,[get/1]}).
-compile({no_auto_import,[put/2]}).
-compile(export_all).
-behaviour(rabbit_auth_cache).
-export([start_link/0,
get/1, put/3, delete/1]).

View File

@ -16,6 +16,7 @@
-module(rabbit_auth_cache_ets_segmented).
-behaviour(gen_server2).
-behaviour(rabbit_auth_cache).
-export([start_link/1,
get/1, put/3, delete/1]).

View File

@ -16,6 +16,7 @@
-module(rabbit_auth_cache_ets_segmented_stateless).
-behaviour(gen_server2).
-behaviour(rabbit_auth_cache).
-export([start_link/1,
get/1, put/3, delete/1]).