File: memcached.c

package info (click to toggle)
hashrat 1.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,984 kB
  • sloc: ansic: 21,832; sh: 369; makefile: 187
file content (70 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download
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
#include "memcached.h" 
 
STREAM *MCS=NULL; 


STREAM *MemcachedConnect(const char *Server)
{
if (StrLen(Server))
{
if (MCS) STREAMClose(MCS);

MCS=STREAMCreate();
if (! STREAMTCPConnect(MCS, Server, 11211,0,0, 0))
{
	STREAMClose(MCS);
	MCS=NULL;
}
}

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);
}