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
*shell
,
24 const memcached_callback_t flag
,
27 Memcached
* ptr
= memcached2Memcached(shell
);
32 case MEMCACHED_CALLBACK_PREFIX_KEY
:
34 return memcached_set_namespace(*ptr
, (char*)data
, data
? strlen((char*)data
) : 0);
37 case MEMCACHED_CALLBACK_USER_DATA
:
39 ptr
->user_data
= const_cast<void *>(data
);
43 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION
:
45 memcached_cleanup_fn func
= *(memcached_cleanup_fn
*)&data
;
46 ptr
->on_cleanup
= func
;
50 case MEMCACHED_CALLBACK_CLONE_FUNCTION
:
52 memcached_clone_fn func
= *(memcached_clone_fn
*)&data
;
57 case MEMCACHED_CALLBACK_GET_FAILURE
:
59 memcached_trigger_key_fn func
= *(memcached_trigger_key_fn
*)&data
;
60 ptr
->get_key_failure
= func
;
64 case MEMCACHED_CALLBACK_DELETE_TRIGGER
:
66 if (data
) // NULL would mean we are disabling.
68 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
))
70 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
73 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_NOREPLY
))
75 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
79 memcached_trigger_delete_key_fn func
= *(memcached_trigger_delete_key_fn
*)&data
;
80 ptr
->delete_trigger
= func
;
84 case MEMCACHED_CALLBACK_MAX
:
85 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid callback supplied"));
88 return MEMCACHED_SUCCESS
;
91 return MEMCACHED_INVALID_ARGUMENTS
;
94 void *memcached_callback_get(memcached_st
*shell
,
95 const memcached_callback_t flag
,
96 memcached_return_t
*error
)
98 Memcached
* ptr
= memcached2Memcached(shell
);
99 memcached_return_t local_error
;
102 error
= &local_error
;
107 *error
= MEMCACHED_INVALID_ARGUMENTS
;
113 case MEMCACHED_CALLBACK_PREFIX_KEY
:
115 *error
= MEMCACHED_SUCCESS
;
118 return (void *)memcached_array_string(ptr
->_namespace
);
123 case MEMCACHED_CALLBACK_USER_DATA
:
125 *error
= ptr
->user_data
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
126 return (void *)ptr
->user_data
;
129 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION
:
131 *error
= ptr
->on_cleanup
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
132 return *(void **)&ptr
->on_cleanup
;
135 case MEMCACHED_CALLBACK_CLONE_FUNCTION
:
137 *error
= ptr
->on_clone
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
138 return *(void **)&ptr
->on_clone
;
141 case MEMCACHED_CALLBACK_GET_FAILURE
:
143 *error
= ptr
->get_key_failure
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
144 return *(void **)&ptr
->get_key_failure
;
147 case MEMCACHED_CALLBACK_DELETE_TRIGGER
:
149 *error
= ptr
->delete_trigger
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
150 return *(void **)&ptr
->delete_trigger
;
153 case MEMCACHED_CALLBACK_MAX
:
157 assert_msg(0, "Invalid callback passed to memcached_callback_get()");
158 *error
= MEMCACHED_FAILURE
;