forked from buaazp/zimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzcache.c
340 lines (280 loc) · 8.94 KB
/
zcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* zimg - high performance image storage and processing system.
* http://zimg.buaa.us
*
* Copyright (c) 2013, Peter Zhao <[email protected]>.
* All rights reserved.
*
* Use and distribution licensed under the BSD license.
* See the LICENSE file for full text.
*
*/
/**
* @file zcache.c
* @brief memcached functions.
* @author 招牌疯子 [email protected]
* @version 1.0
* @date 2013-07-19
*/
#include "zcache.h"
#include "zlog.h"
extern struct setting settings;
int exist_cache(const char *key);
int find_cache(const char *key, char *value);
int set_cache(const char *key, const char *value);
int find_cache_bin(const char *key, char **value_ptr, size_t *len);
int set_cache_bin(const char *key, const char *value, const size_t len);
int del_cache(const char *key);
/**
* @brief exist_cache Check a key is exist in memcached.
*
* @param key The string of the key.
*
* @return 1 for yes and -1 for no.
*/
int exist_cache(const char *key)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
size_t valueLen;
uint32_t flags;
memcached_return rc;
memcached_get(memc, key, strlen(key), &valueLen, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Cache Key[%s] Exist.", key);
rst = 1;
}
else
{
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
}
memcached_free(memc);
return rst;
}
/**
* @brief find_cache Connect to a memcached server and find a key's value.
*
* @param key The string of the key you want to find.
* @param value It contains the string of the key's value.
*
* @return 1 for success and -1 for fail.
*/
int find_cache(const char *key, char *value)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
size_t valueLen;
uint32_t flags;
memcached_return rc;
char *pvalue = memcached_get(memc, key, strlen(key), &valueLen, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Cache Find Key[%s] Value: %s", key, pvalue);
strcpy(value, pvalue);
free(pvalue);
rst = 1;
}
else if (rc == MEMCACHED_NOTFOUND)
{
LOG_PRINT(LOG_WARNING, "Cache Key[%s] Not Find!", key);
rst = -1;
}
else
{
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
}
memcached_free(memc);
return rst;
}
/**
* @brief set_cache Set a key with the value input.
*
* @param key The key you want to set a new value.
* @param value The value of the key.
*
* @return 1 for success and -1 for fail.
*/
int set_cache(const char *key, const char *value)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
memcached_return rc;
rc = memcached_set(memc, key, strlen(key), value, strlen(value), 0, 0);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Cache Set Successfully. Key[%s] Value: %s", key, value);
rst = 1;
}
else
{
LOG_PRINT(LOG_WARNING, "Cache Set(Key: %s Value: %s) Failed!", key, value);
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
rst = -1;
}
memcached_free(memc);
return rst;
}
/**
* @brief find_cache_bin Find a key's BINARY value.
*
* @param key The key you want to find.
* @param value_ptr It will be alloc and contains the binary value.
* @param len It will change to the length of the value.
*
* @return 1 for success and -1 for fail.
*/
int find_cache_bin(const char *key, char **value_ptr, size_t *len)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
uint32_t flags;
memcached_return rc;
*value_ptr = memcached_get(memc, key, strlen(key), len, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Binary Cache Find Key[%s], Len: %d.", key, *len);
rst = 1;
}
else if (rc == MEMCACHED_NOTFOUND)
{
LOG_PRINT(LOG_WARNING, "Binary Cache Key[%s] Not Find!", key);
rst = -1;
}
else
{
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
}
memcached_free(memc);
return rst;
}
/**
* @brief set_cache_bin Set a new BINARY value of a key.
*
* @param key The key.
* @param value A char * buffer you want to set.
* @param len The length of the buffer above,
*
* @return 1 for success and -1 for fial.
*/
int set_cache_bin(const char *key, const char *value, const size_t len)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
memcached_return rc;
rc = memcached_set(memc, key, strlen(key), value, len, 0, 0);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Binary Cache Set Successfully. Key[%s] Len: %d.", key, len);
rst = 1;
settings.cache_on = true;
}
else
{
LOG_PRINT(LOG_WARNING, "Binary Cache Set(Key: %s ) Failed!", key);
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
rst = -1;
settings.cache_on = false;
}
memcached_free(memc);
return rst;
}
/**
* @brief del_cache This function delete a key and its value in memcached.
*
* @param key The key.
*
* @return 1 for success and -1 for fail.
*/
int del_cache(const char *key)
{
int rst = -1;
if(settings.cache_on == false)
return rst;
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
memcached_return rc;
rc = memcached_delete(memc, key, strlen(key), 0);
if (rc == MEMCACHED_SUCCESS)
{
LOG_PRINT(LOG_INFO, "Cache Key[%s] Delete Successfully.", key);
rst = 1;
}
else
{
LOG_PRINT(LOG_WARNING, "Cache Key[%s] Delete Failed!", key);
const char *str_rc = memcached_strerror(memc, rc);
LOG_PRINT(LOG_INFO, "Cache Result: %s", str_rc);
rst = -1;
}
memcached_free(memc);
return rst;
}