2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Change any of the possible callbacks.
12 #include <libmemcached/common.h>
13 #include <sys/types.h>
15 #ifndef __INTEL_COMPILER
16 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
20 These functions provide data and function callback support
23 memcached_return_t
memcached_callback_set(memcached_st
*ptr
,
24 const memcached_callback_t flag
,
29 case MEMCACHED_CALLBACK_PREFIX_KEY
:
31 return memcached_set_namespace(ptr
, (char*)data
, data
? strlen((char*)data
) : 0);
34 case MEMCACHED_CALLBACK_USER_DATA
:
36 ptr
->user_data
= const_cast<void *>(data
);
40 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION
:
42 memcached_cleanup_fn func
= *(memcached_cleanup_fn
*)&data
;
43 ptr
->on_cleanup
= func
;
47 case MEMCACHED_CALLBACK_CLONE_FUNCTION
:
49 memcached_clone_fn func
= *(memcached_clone_fn
*)&data
;
54 case MEMCACHED_CALLBACK_GET_FAILURE
:
56 memcached_trigger_key_fn func
= *(memcached_trigger_key_fn
*)&data
;
57 ptr
->get_key_failure
= func
;
61 case MEMCACHED_CALLBACK_DELETE_TRIGGER
:
63 if (data
) // NULL would mean we are disabling.
65 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
))
67 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
70 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_NOREPLY
))
72 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
76 memcached_trigger_delete_key_fn func
= *(memcached_trigger_delete_key_fn
*)&data
;
77 ptr
->delete_trigger
= func
;
81 case MEMCACHED_CALLBACK_MAX
:
82 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid callback supplied"));
85 return MEMCACHED_SUCCESS
;
88 void *memcached_callback_get(memcached_st
*ptr
,
89 const memcached_callback_t flag
,
90 memcached_return_t
*error
)
92 memcached_return_t local_error
;
100 *error
= MEMCACHED_INVALID_ARGUMENTS
;
106 case MEMCACHED_CALLBACK_PREFIX_KEY
:
108 *error
= MEMCACHED_SUCCESS
;
111 return (void *)memcached_array_string(ptr
->_namespace
);
116 case MEMCACHED_CALLBACK_USER_DATA
:
118 *error
= ptr
->user_data
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
119 return (void *)ptr
->user_data
;
122 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION
:
124 *error
= ptr
->on_cleanup
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
125 return *(void **)&ptr
->on_cleanup
;
128 case MEMCACHED_CALLBACK_CLONE_FUNCTION
:
130 *error
= ptr
->on_clone
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
131 return *(void **)&ptr
->on_clone
;
134 case MEMCACHED_CALLBACK_GET_FAILURE
:
136 *error
= ptr
->get_key_failure
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
137 return *(void **)&ptr
->get_key_failure
;
140 case MEMCACHED_CALLBACK_DELETE_TRIGGER
:
142 *error
= ptr
->delete_trigger
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
143 return *(void **)&ptr
->delete_trigger
;
146 case MEMCACHED_CALLBACK_MAX
:
150 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
151 *error
= MEMCACHED_FAILURE
;