-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmemcached.c
74 lines (54 loc) · 1.49 KB
/
memcached.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
#include "memcached.h"
STREAM *MCS=NULL;
STREAM *MemcachedConnect(const char *Server)
{
char *Tempstr=NULL;
if (StrLen(Server))
{
if (MCS) STREAMClose(MCS);
Tempstr=MCopyStr(Tempstr, "tcp:", Server, ":11211", NULL);
MCS=STREAMOpen(Tempstr, "");
if (MCS)
{
STREAMClose(MCS);
MCS=NULL;
}
}
Destroy(Tempstr);
return(MCS);
}
int MemcachedSet(const char *Key, int TTL, const char *Value)
{
char *Tempstr=NULL;
int result=FALSE;
if (STREAMIsConnected(MCS))
{
Tempstr=FormatStr(Tempstr,"set %s 0 %d %d\r\n%s\r\n",Key,TTL,StrLen(Value),Value);
STREAMWriteLine(Tempstr,MCS);
Tempstr=STREAMReadLine(Tempstr,MCS);
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr) && (strcmp(Tempstr,"STORED")==0)) result=TRUE;
}
Destroy(Tempstr);
return(result);
}
char *MemcachedGet(char *RetStr, const char *Key)
{
char *Tempstr=NULL;
if (STREAMIsConnected(MCS))
{
RetStr=CopyStr(RetStr,"");
Tempstr=FormatStr(Tempstr,"get %s\r\n",Key);
STREAMWriteLine(Tempstr,MCS);
Tempstr=STREAMReadLine(Tempstr,MCS);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (strcmp(Tempstr,"END")==0) break;
if (strncmp(Tempstr,"VALUE ",6) !=0) RetStr=CopyStr(RetStr,Tempstr);
Tempstr=STREAMReadLine(Tempstr,MCS);
}
}
Destroy(Tempstr);
return(RetStr);
}