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
;
101 *error
= MEMCACHED_INVALID_ARGUMENTS
;
107 case MEMCACHED_CALLBACK_PREFIX_KEY
:
109 *error
= MEMCACHED_SUCCESS
;
112 return (void *)memcached_array_string(ptr
->_namespace
);
117 case MEMCACHED_CALLBACK_USER_DATA
:
119 *error
= ptr
->user_data
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
120 return (void *)ptr
->user_data
;
123 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION
:
125 *error
= ptr
->on_cleanup
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
126 return *(void **)&ptr
->on_cleanup
;
129 case MEMCACHED_CALLBACK_CLONE_FUNCTION
:
131 *error
= ptr
->on_clone
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
132 return *(void **)&ptr
->on_clone
;
135 case MEMCACHED_CALLBACK_GET_FAILURE
:
137 *error
= ptr
->get_key_failure
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
138 return *(void **)&ptr
->get_key_failure
;
141 case MEMCACHED_CALLBACK_DELETE_TRIGGER
:
143 *error
= ptr
->delete_trigger
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
144 return *(void **)&ptr
->delete_trigger
;
147 case MEMCACHED_CALLBACK_MAX
:
151 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
152 *error
= MEMCACHED_FAILURE
;