Annotation of parser3/src/classes/memcached.C, revision 1.4
1.1 moko 1: /** @file
2: Parser: memcached class.
3:
4: Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
5: Authors:
6: Ivan Poluyanov <ivan-poluyanov@yandex.ru>
7: Artem Stepanov <timofei1394@thesecure.in>
8: */
9:
10: #include "pa_vmethod_frame.h"
11:
12: #include "pa_request.h"
13: #include "pa_vstring.h"
1.2 moko 14: #include "pa_vtable.h"
1.4 ! moko 15: #include "pa_vbool.h"
1.1 moko 16: #include "pa_vmemcached.h"
17:
1.4 ! moko 18: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.3 2012-04-22 22:06:50 moko Exp $";
1.1 moko 19:
20: class MMemcached: public Methoded {
21: public: // VStateless_class
22: Value* create_new_value(Pool&) { return new VMemcached(); }
23: public:
24: MMemcached();
25: };
26:
27: DECLARE_CLASS_VAR(memcached, new MMemcached, 0);
28:
29: static void _open(Request& r, MethodParams& params) {
1.3 moko 30: Value& param_value=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
31: VMemcached& self=GET_SELF(r, VMemcached);
32:
1.2 moko 33: int ttl=0;
34:
35: if(params.count()>1)
36: ttl=params.as_int(1, "default expiration must be int", r);
37:
1.3 moko 38: if(HashStringValue* options=param_value.get_hash()){
39: String result;
40: for(HashStringValue::Iterator i(*options); i; i.next()){
41: result << (result.is_empty() ? "--" : " --") << i.key();
42: const String& value=i.value()->as_string();
43: if(!value.is_empty())
44: result << "=" << value;
45: }
46: self.open(result, ttl);
47: } else {
48: const String& connect_string=params.as_string(0, "param must be connection string or options hash");
49: self.open_parse(connect_string, ttl);
50: }
1.2 moko 51: }
52:
53: static void _flush(Request& r, MethodParams& params) {
54: int ttl=0;
55:
56: if(params.count()>0)
57: params.as_int(0, "expiration must be int", r);
58:
59: VMemcached& self=GET_SELF(r, VMemcached);
60: self.flush(ttl);
61: }
62:
63: static void _mget(Request& r, MethodParams& params) {
64: Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
65:
66: VMemcached& self=GET_SELF(r, VMemcached);
67:
68: if(param.is_string()){
69:
70: ArrayString keys(params.count());
71:
72: for(size_t i=0; i<params.count(); i++) {
73: keys+=¶ms.as_string(i, "key must be string");
74: }
75:
76: r.write_no_lang(self.mget(keys));
77: } else {
78: Table* table=param.get_table();
79: if(table==0){
80: throw Exception("memcached", 0, "key must be string or table");
81: }
82:
83: ArrayString keys(table->count());
84:
85: for(size_t i=0; i<table->count(); i++) {
86: keys+=table->get(i)->get(0);
87: }
88:
89: r.write_no_lang(self.mget(keys));
90: }
1.1 moko 91: }
92:
1.4 ! moko 93: static void _add(Request& r, MethodParams& params) {
! 94: const String& key=params.as_string(0, "key must be string");
! 95:
! 96: VMemcached& self=GET_SELF(r, VMemcached);
! 97: r.write_no_lang(VBool::get(self.add(key, params.get(1))));
! 98: }
! 99:
1.1 moko 100: static void _delete(Request& r, MethodParams& params) {
101: const String& key=params.as_string(0, "key must be string");
102:
103: VMemcached& self=GET_SELF(r, VMemcached);
104: self.remove(key);
105: }
106:
107: MMemcached::MMemcached() : Methoded("memcached") {
1.2 moko 108: add_native_method("open", Method::CT_DYNAMIC, _open, 1, 2);
109: add_native_method("flush", Method::CT_DYNAMIC, _flush, 1, 1);
110: add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.4 ! moko 111: add_native_method("add", Method::CT_DYNAMIC, _add, 2, 2);
1.1 moko 112: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
113: }
E-mail: