25 #define LOG_PREFIX "cache - memcached"
27 #include <libmemcached/memcached.h>
29 #include <freeradius-devel/server/base.h>
30 #include <freeradius-devel/server/module_rlm.h>
31 #include <freeradius-devel/util/debug.h>
32 #include <freeradius-devel/util/value.h>
34 #include "../../rlm_cache.h"
35 #include "../../serialize.h"
71 memcached_return_t ret;
73 sandle = memcached(driver->
options, talloc_array_length(driver->
options) -1);
75 ERROR(
"Failed creating memcached connection");
81 if (ret != MEMCACHED_SUCCESS) {
82 ERROR(
"%s: %s", memcached_strerror(sandle, ret), memcached_last_error_message(sandle));
84 memcached_free(sandle);
88 ret = memcached_version(sandle);
89 if (ret != MEMCACHED_SUCCESS) {
90 ERROR(
"%s: %s", memcached_strerror(sandle, ret), memcached_last_error_message(sandle));
112 memcached_return_t ret;
120 ret = libmemcached_check_configuration(driver->
options, talloc_array_length(driver->
options) -1,
122 if (ret != MEMCACHED_SUCCESS) {
128 buffer,
"modules.rlm_cache.pool", NULL);
129 if (!driver->
pool)
return -1;
133 if (
config->max_entries > 0) {
134 ERROR(
"max_entries is not supported by this driver");
142 INFO(
"%s", memcached_lib_version());
165 memcached_return_t mret;
174 from_store = memcached_get(mandle->
handle, (
char const *)key->vb_strvalue, key->vb_length, &len, &flags, &mret);
176 if (mret == MEMCACHED_NOTFOUND)
return CACHE_MISS;
178 RERROR(
"Failed retrieving entry: %s: %s", memcached_strerror(mandle->
handle, mret),
179 memcached_last_error_message(mandle->
handle));
183 RDEBUG2(
"Retrieved %zu bytes from memcached", len);
196 RERROR(
"Failed copying key");
214 memcached_return_t ret;
219 pool = talloc_pool(NULL, 1024);
228 ret = memcached_set(mandle->
handle, (
char const *)c->
key.vb_strvalue, c->
key.vb_length,
229 to_store ? to_store :
"",
232 if (ret != MEMCACHED_SUCCESS) {
233 RERROR(
"Failed storing entry: %s: %s", memcached_strerror(mandle->
handle, ret),
234 memcached_last_error_message(mandle->
handle));
251 memcached_return_t ret;
253 ret = memcached_delete(mandle->
handle, (
char const *)key->vb_strvalue, key->vb_length, 0);
255 case MEMCACHED_SUCCESS:
258 case MEMCACHED_DATA_DOES_NOT_EXIST:
262 RERROR(
"Failed deleting entry: %s", memcached_last_error_message(mandle->
handle));
325 .name =
"cache_memcached",
static int const char char buffer[256]
#define CONF_PARSER_TERMINATOR
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Defines a CONF_PAIR to C data type mapping.
A section grouping multiple CONF_PAIR.
static fr_time_delta_t timeout
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
module_instance_t * mi
Instance of the module being instantiated.
Temporary structure to hold arguments for instantiation calls.
fr_pool_t * module_rlm_connection_pool_init(CONF_SECTION *module, void *opaque, fr_pool_connection_create_t c, fr_pool_connection_alive_t a, char const *log_prefix, char const *trigger_prefix, fr_pair_list_t *trigger_args)
Initialise a module specific connection pool.
void fr_pool_connection_release(fr_pool_t *pool, request_t *request, void *conn)
Release a connection.
void * fr_pool_connection_get(fr_pool_t *pool, request_t *request)
Reserve a connection in the connection pool.
void * fr_pool_connection_reconnect(fr_pool_t *pool, request_t *request, void *conn)
Reconnect a suspected inviable connection.
static const conf_parser_t config[]
fr_value_box_t key
Key used to identify entry.
module_t common
Common fields for all loadable modules.
@ CACHE_ERROR
Fatal error.
@ CACHE_OK
Cache entry found/updated.
@ CACHE_MISS
Cache entry notfound.
fr_unix_time_t expires
When the entry expires.
Configuration for the rlm_cache module.
static cache_status_t cache_entry_expire(UNUSED rlm_cache_config_t const *config, UNUSED void *instance, request_t *request, void *handle, fr_value_box_t const *key)
Call delete the cache entry from memcached.
static int _mod_conn_free(rlm_cache_memcached_handle_t *mandle)
Free a connection handle.
static int mod_load(void)
static int mod_conn_reconnect(void **handle, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request)
Reconnect a memcached handle.
static cache_status_t cache_entry_insert(UNUSED rlm_cache_config_t const *config, UNUSED void *instance, request_t *request, void *handle, const rlm_cache_entry_t *c)
Insert a new entry into the data store.
static void mod_conn_release(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, rlm_cache_handle_t *handle)
Release a memcached handle.
static void cache_entry_free(rlm_cache_entry_t *c)
Locate a cache entry in memcached.
rlm_cache_driver_t rlm_cache_memcached
static cache_status_t cache_entry_find(rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, UNUSED void *instance, request_t *request, void *handle, fr_value_box_t const *key)
Locate a cache entry in memcached.
static const conf_parser_t driver_config[]
char const * options
Connection options.
static void * mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout)
Create a new memcached handle.
static int mod_conn_get(void **handle, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request)
Get a memcached handle.
static int mod_instantiate(module_inst_ctx_t const *mctx)
Create a new rlm_cache_memcached instance.
static int instantiate(module_inst_ctx_t const *mctx)
int cache_serialize(TALLOC_CTX *ctx, char **out, rlm_cache_entry_t const *c)
Serialize a cache entry as a humanly readable string.
int cache_deserialize(rlm_cache_entry_t *c, fr_dict_t const *dict, char *in, ssize_t inlen)
Converts a serialized cache entry back into a structure.
char const * name
Instance name e.g. user_database.
CONF_SECTION * conf
Module's instance configuration.
void * data
Module's instance data.
module_instance_t const * parent
Parent module's instance (if any).
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child)
Link two different parent and child contexts, so the child is freed before the parent.
static int64_t fr_unix_time_to_sec(fr_unix_time_t delta)
static int64_t fr_time_delta_to_msec(fr_time_delta_t delta)
A time delta, a difference in time measured in nanoseconds.
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
static size_t char ** out