/[projects]/misc/memcached/CppProducer/producer.cpp
ViewVC logotype

Contents of /misc/memcached/CppProducer/producer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 644 - (show annotations) (download)
Fri Apr 9 22:02:22 2010 UTC (14 years, 1 month ago) by torben
File size: 773 byte(s)
Added memcached producer/consumer example
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4
5 #include "memcached.hh"
6
7 using namespace std;
8
9 int count = 0;
10 const int SLOT_COUNT = 10;
11
12 string produce() {
13 stringstream oss;
14 oss << "packet " << count++ << " produced on " << time(0) ;
15 return oss.str();
16 }
17
18 string gen_key(int i) {
19 stringstream oss;
20 oss << "slot:" << i;
21 return oss.str();
22 }
23
24 int main() {
25 Memcached memc;
26 memc.server_add("localhost");
27
28 unsigned int len;
29
30 while (1) {
31 for (int i=0; i<SLOT_COUNT; i++) {
32 string key = gen_key(i);
33 char* tmp = memc.get(key,&len);
34 if (tmp == NULL) { //slot is empty
35 cout << "Filling slot " << i << endl;
36 string value = produce();
37 memc.set(key, value.c_str(), value.length() +1);
38 }
39
40 }
41 cout << "----" << endl;
42 sleep(5);
43 }
44 }

  ViewVC Help
Powered by ViewVC 1.1.20