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/slab.h>
33#include <freeradius-devel/util/value.h>
35#include "../../rlm_cache.h"
36#include "../../serialize.h"
54 memcached_slab_list_t *
slab;
87 memcached_return_t ret;
89 sandle = memcached(driver->
options, talloc_array_length(driver->
options) -1);
91 ERROR(
"Failed creating memcached connection");
96 if (ret != MEMCACHED_SUCCESS) {
97 ERROR(
"%s: %s", memcached_strerror(sandle, ret), memcached_last_error_message(sandle));
99 memcached_free(sandle);
103 ret = memcached_version(sandle);
104 if (ret != MEMCACHED_SUCCESS) {
105 ERROR(
"%s: %s", memcached_strerror(sandle, ret), memcached_last_error_message(sandle));
125 memcached_return_t ret;
129 ret = libmemcached_check_configuration(driver->
options, talloc_array_length(driver->
options) -1,
131 if (ret != MEMCACHED_SUCCESS) {
136 if (
inst->config.max_entries > 0) {
137 ERROR(
"max_entries is not supported by this driver");
141 driver->
mi = mctx->
mi;
147 INFO(
"%s", memcached_lib_version());
170 memcached_return_t mret;
179 from_store = memcached_get(mandle->
handle, (
char const *)key->vb_strvalue, key->vb_length, &len, &flags, &mret);
181 if (mret == MEMCACHED_NOTFOUND)
return CACHE_MISS;
183 RERROR(
"Failed retrieving entry: %s: %s", memcached_strerror(mandle->
handle, mret),
184 memcached_last_error_message(mandle->
handle));
188 RDEBUG2(
"Retrieved %zu bytes from memcached", len);
192 map_list_init(&c->
maps);
202 RERROR(
"Failed copying key");
220 memcached_return_t ret;
225 pool = talloc_pool(NULL, 1024);
234 ret = memcached_set(mandle->
handle, (
char const *)c->
key.vb_strvalue, c->
key.vb_length,
235 to_store ? to_store :
"",
238 if (ret != MEMCACHED_SUCCESS) {
239 RERROR(
"Failed storing entry: %s: %s", memcached_strerror(mandle->
handle, ret),
240 memcached_last_error_message(mandle->
handle));
257 memcached_return_t ret;
259 ret = memcached_delete(mandle->
handle, (
char const *)key->vb_strvalue, key->vb_length, 0);
261 case MEMCACHED_SUCCESS:
264 case MEMCACHED_DATA_DOES_NOT_EXIST:
268 RERROR(
"Failed deleting entry: %s", memcached_last_error_message(mandle->
handle));
284 mandle = memcached_slab_reserve(t->
slab);
301 memcached_slab_release(handle);
316 mandle = memcached_slab_reserve(t->
slab);
334 ERROR(
"Connection handle pool instantiation failed");
352 .name =
"cache_memcached",
static int const char char buffer[256]
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#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
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Defines a CONF_PAIR to C data type mapping.
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
fr_event_list_t * el
Event list to register any IO handlers and timers against.
void * thread
Thread instance data.
module_instance_t const * mi
Instance of the module being instantiated.
module_instance_t * mi
Instance of the module being instantiated.
Temporary structure to hold arguments for instantiation calls.
Temporary structure to hold arguments for thread_instantiation calls.
static const conf_parser_t config[]
fr_value_box_t key
Key used to identify entry.
map_list_t maps
Head of the maps list.
module_t common
Common fields for all loadable modules.
@ CACHE_ERROR
Fatal error.
@ CACHE_RECONNECT
Handle needs to be reconnected.
@ 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 memcached_conn_init(rlm_cache_memcached_handle_t *mandle, void *uctx)
Create a new 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 cache_entry_free(rlm_cache_entry_t *c)
Locate a cache entry in memcached.
rlm_cache_driver_t rlm_cache_memcached
memcached_slab_list_t * slab
static void mod_conn_release(UNUSED rlm_cache_config_t const *config, UNUSED void *instance, UNUSED request_t *request, rlm_cache_handle_t *handle)
Release a memcached handle.
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 int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
static const conf_parser_t driver_config[]
rlm_cache_memcached_t const * inst
char const * options
Connection options.
static conf_parser_t reuse_memcached_config[]
static int mod_conn_reconnect(void **handle, UNUSED rlm_cache_config_t const *config, void *instance, UNUSED request_t *request)
Reconnect a memcached handle.
module_instance_t const * mi
static int mod_thread_detach(module_thread_inst_ctx_t const *mctx)
static int mod_instantiate(module_inst_ctx_t const *mctx)
Create a new rlm_cache_memcached instance.
static int mod_conn_get(void **handle, UNUSED rlm_cache_config_t const *config, void *instance, UNUSED request_t *request)
Get a memcached handle.
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(request_t *request, rlm_cache_entry_t *c, fr_dict_t const *dict, char *in, ssize_t inlen)
Converts a serialized cache entry back into a structure.
size_t inst_size
Size of the module's instance data.
void * data
Module's instance data.
module_instance_t const * parent
Parent module's instance (if any).
void * data
Thread specific instance data.
static module_thread_instance_t * module_thread(module_instance_t const *mi)
Retrieve module/thread specific instance for a module.
#define FR_SLAB_FUNCS(_name, _type)
Define type specific wrapper functions for slabs and slab elements.
#define FR_SLAB_TYPES(_name, _type)
Define type specific wrapper structs for slabs and slab elements.
#define FR_SLAB_CONFIG_CONF_PARSER
conf_parser_t entries to populate user configurable slab values
Tuneable parameters for slabs.
eap_aka_sim_process_conf_t * inst
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