Annotation of parser3/src/classes/memcached.C, revision 1.3
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.1 moko 15: #include "pa_vmemcached.h"
16:
1.3 ! moko 17: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.2 2012-03-23 22:33:23 moko Exp $";
1.1 moko 18:
19: class MMemcached: public Methoded {
20: public: // VStateless_class
21: Value* create_new_value(Pool&) { return new VMemcached(); }
22: public:
23: MMemcached();
24: };
25:
26: DECLARE_CLASS_VAR(memcached, new MMemcached, 0);
27:
28: static void _open(Request& r, MethodParams& params) {
1.3 ! moko 29: Value& param_value=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
! 30: VMemcached& self=GET_SELF(r, VMemcached);
! 31:
1.2 moko 32: int ttl=0;
33:
34: if(params.count()>1)
35: ttl=params.as_int(1, "default expiration must be int", r);
36:
1.3 ! moko 37: if(HashStringValue* options=param_value.get_hash()){
! 38: String result;
! 39: for(HashStringValue::Iterator i(*options); i; i.next()){
! 40: result << (result.is_empty() ? "--" : " --") << i.key();
! 41: const String& value=i.value()->as_string();
! 42: if(!value.is_empty())
! 43: result << "=" << value;
! 44: }
! 45: self.open(result, ttl);
! 46: } else {
! 47: const String& connect_string=params.as_string(0, "param must be connection string or options hash");
! 48: self.open_parse(connect_string, ttl);
! 49: }
1.2 moko 50: }
51:
52: static void _flush(Request& r, MethodParams& params) {
53: int ttl=0;
54:
55: if(params.count()>0)
56: params.as_int(0, "expiration must be int", r);
57:
58: VMemcached& self=GET_SELF(r, VMemcached);
59: self.flush(ttl);
60: }
61:
62: static void _mget(Request& r, MethodParams& params) {
63: Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
64:
65: VMemcached& self=GET_SELF(r, VMemcached);
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:
92: static void _delete(Request& r, MethodParams& params) {
93: const String& key=params.as_string(0, "key must be string");
94:
95: VMemcached& self=GET_SELF(r, VMemcached);
96: self.remove(key);
97: }
98:
99: MMemcached::MMemcached() : Methoded("memcached") {
1.2 moko 100: add_native_method("open", Method::CT_DYNAMIC, _open, 1, 2);
101: add_native_method("flush", Method::CT_DYNAMIC, _flush, 1, 1);
102: add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.1 moko 103: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
104: }
E-mail: