Annotation of parser3/src/classes/memcached.C, revision 1.5

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.5     ! moko       18: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.4 2012-04-24 22:41:09 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()){
                     35:                String result;
                     36:                for(HashStringValue::Iterator i(*options); i; i.next()){
                     37:                        result << (result.is_empty() ? "--" : " --") << i.key();
                     38:                        const String& value=i.value()->as_string();
                     39:                        if(!value.is_empty())
                     40:                                result << "=" << value;
                     41:                }
                     42:                self.open(result, ttl);
                     43:        } else {
                     44:                const String& connect_string=params.as_string(0, "param must be connection string or options hash");
                     45:                self.open_parse(connect_string, ttl);
                     46:        }
1.2       moko       47: }
                     48: 
                     49: static void _flush(Request& r, MethodParams& params) {
1.5     ! moko       50:        VMemcached& self=GET_SELF(r, VMemcached);
        !            51:        time_t ttl=(params.count()>0) ? params.as_int(0, "expiration must be int", r) : 0;
1.2       moko       52: 
                     53:        self.flush(ttl);
                     54: }
                     55: 
                     56: static void _mget(Request& r, MethodParams& params) {
1.5     ! moko       57:        VMemcached& self=GET_SELF(r, VMemcached);
1.2       moko       58:        Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
                     59: 
                     60:        if(param.is_string()){
                     61: 
                     62:                ArrayString keys(params.count());
                     63:                
                     64:                for(size_t i=0; i<params.count(); i++) {
                     65:                        keys+=&params.as_string(i, "key must be string");
                     66:                }
                     67: 
                     68:                r.write_no_lang(self.mget(keys));
                     69:        } else {
                     70:                Table* table=param.get_table();
                     71:                if(table==0){
                     72:                        throw Exception("memcached", 0, "key must be string or table");
                     73:                }
                     74:                
                     75:                ArrayString keys(table->count());
                     76:                
                     77:                for(size_t i=0; i<table->count(); i++) {
                     78:                        keys+=table->get(i)->get(0);
                     79:                }
                     80: 
                     81:                r.write_no_lang(self.mget(keys));
                     82:        }
1.1       moko       83: }
                     84: 
1.4       moko       85: static void _add(Request& r, MethodParams& params) {
1.5     ! moko       86:        VMemcached& self=GET_SELF(r, VMemcached);
1.4       moko       87:        const String& key=params.as_string(0, "key must be string");
                     88: 
1.5     ! moko       89:        r.write_no_lang(VBool::get(self.add(key, &params.as_no_junction(1, PARAM_MUST_NOT_BE_CODE))));
1.4       moko       90: }
                     91: 
1.1       moko       92: static void _delete(Request& r, MethodParams& params) {
1.5     ! moko       93:        VMemcached& self=GET_SELF(r, VMemcached);
1.1       moko       94:        const String& key=params.as_string(0, "key must be string");
                     95: 
                     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);
1.5     ! moko      101:        add_native_method("flush", Method::CT_DYNAMIC, _flush, 0, 1);
1.2       moko      102:        add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.4       moko      103:        add_native_method("add", Method::CT_DYNAMIC, _add, 2, 2);
1.1       moko      104:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
                    105: }

E-mail: