Annotation of parser3/src/classes/memcached.C, revision 1.12
1.1 moko 1: /** @file
2: Parser: memcached class.
3:
1.12 ! moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 moko 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.12 ! moko 18: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.11 2013/12/02 21:44:47 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.5 moko 30: VMemcached& self=GET_SELF(r, VMemcached);
1.3 moko 31: Value& param_value=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
1.5 moko 32: time_t ttl=params.count()>1 ? params.as_int(1, "default expiration must be int", r) : 0;
1.2 moko 33:
1.3 moko 34: if(HashStringValue* options=param_value.get_hash()){
1.11 moko 35: bool connect=true;
1.3 moko 36: String result;
37: for(HashStringValue::Iterator i(*options); i; i.next()){
1.11 moko 38: if(i.key() == "skip-connect"){
39: connect=!i.value()->as_bool();
40: } else if(Value *b=i.value()->as("bool")){
1.6 moko 41: if(b->as_bool())
42: result << (result.is_empty() ? "--" : " --") << i.key();
43: } else {
44: const String& value=i.value()->as_string();
45: if(!value.is_empty())
1.7 moko 46: result << (result.is_empty() ? "--" : " --") << i.key() << "=" << value;
1.6 moko 47: }
1.3 moko 48: }
1.11 moko 49: self.open(result, ttl, connect);
1.3 moko 50: } else {
51: const String& connect_string=params.as_string(0, "param must be connection string or options hash");
52: self.open_parse(connect_string, ttl);
53: }
1.2 moko 54: }
55:
1.8 moko 56: static void _clear(Request& r, MethodParams& params) {
1.5 moko 57: VMemcached& self=GET_SELF(r, VMemcached);
58: time_t ttl=(params.count()>0) ? params.as_int(0, "expiration must be int", r) : 0;
1.2 moko 59:
60: self.flush(ttl);
61: }
62:
63: static void _mget(Request& r, MethodParams& params) {
1.5 moko 64: VMemcached& self=GET_SELF(r, VMemcached);
1.2 moko 65: Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
66:
67: if(param.is_string()){
68:
69: ArrayString keys(params.count());
70:
71: for(size_t i=0; i<params.count(); i++) {
72: keys+=¶ms.as_string(i, "key must be string");
73: }
74:
75: r.write_no_lang(self.mget(keys));
76: } else {
77: Table* table=param.get_table();
78: if(table==0){
79: throw Exception("memcached", 0, "key must be string or table");
80: }
81:
82: ArrayString keys(table->count());
83:
84: for(size_t i=0; i<table->count(); i++) {
85: keys+=table->get(i)->get(0);
86: }
87:
88: r.write_no_lang(self.mget(keys));
89: }
1.1 moko 90: }
91:
1.4 moko 92: static void _add(Request& r, MethodParams& params) {
1.5 moko 93: VMemcached& self=GET_SELF(r, VMemcached);
1.4 moko 94: const String& key=params.as_string(0, "key must be string");
95:
1.5 moko 96: r.write_no_lang(VBool::get(self.add(key, ¶ms.as_no_junction(1, PARAM_MUST_NOT_BE_CODE))));
1.4 moko 97: }
98:
1.1 moko 99: static void _delete(Request& r, MethodParams& params) {
1.5 moko 100: VMemcached& self=GET_SELF(r, VMemcached);
1.1 moko 101: const String& key=params.as_string(0, "key must be string");
102:
103: self.remove(key);
104: }
105:
1.10 moko 106: static void _release(Request& r, MethodParams&) {
1.9 moko 107: VMemcached& self=GET_SELF(r, VMemcached);
108:
109: self.quit();
110: }
111:
1.1 moko 112: MMemcached::MMemcached() : Methoded("memcached") {
1.2 moko 113: add_native_method("open", Method::CT_DYNAMIC, _open, 1, 2);
1.8 moko 114: add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 1);
1.2 moko 115: add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.4 moko 116: add_native_method("add", Method::CT_DYNAMIC, _add, 2, 2);
1.1 moko 117: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.9 moko 118: add_native_method("release", Method::CT_DYNAMIC, _release, 0, 0);
1.1 moko 119: }
E-mail: