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

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.2     ! moko       17: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.1 2012-03-19 22:19:27 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) {
                     29:        const String& connect_string=params.as_string(0, "connection string must be string");
1.2     ! moko       30:        int ttl=0;
        !            31:        
        !            32:        if(params.count()>1)
        !            33:                ttl=params.as_int(1, "default expiration must be int", r);
        !            34: 
1.1       moko       35:        VMemcached& self=GET_SELF(r, VMemcached);
1.2     ! moko       36:        self.open(connect_string, ttl);
        !            37: }
        !            38: 
        !            39: static void _flush(Request& r, MethodParams& params) {
        !            40:        int ttl=0;
        !            41: 
        !            42:        if(params.count()>0)
        !            43:                params.as_int(0, "expiration must be int", r);
        !            44: 
        !            45:        VMemcached& self=GET_SELF(r, VMemcached);
        !            46:        self.flush(ttl);
        !            47: }
        !            48: 
        !            49: static void _mget(Request& r, MethodParams& params) {
        !            50:        Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
        !            51: 
        !            52:        VMemcached& self=GET_SELF(r, VMemcached);
        !            53: 
        !            54:        if(param.is_string()){
        !            55: 
        !            56:                ArrayString keys(params.count());
        !            57:                
        !            58:                for(size_t i=0; i<params.count(); i++) {
        !            59:                        keys+=&params.as_string(i, "key must be string");
        !            60:                }
        !            61: 
        !            62:                r.write_no_lang(self.mget(keys));
        !            63:        } else {
        !            64:                Table* table=param.get_table();
        !            65:                if(table==0){
        !            66:                        throw Exception("memcached", 0, "key must be string or table");
        !            67:                }
        !            68:                
        !            69:                ArrayString keys(table->count());
        !            70:                
        !            71:                for(size_t i=0; i<table->count(); i++) {
        !            72:                        keys+=table->get(i)->get(0);
        !            73:                }
        !            74: 
        !            75:                r.write_no_lang(self.mget(keys));
        !            76:        }
1.1       moko       77: }
                     78: 
                     79: static void _delete(Request& r, MethodParams& params) {
                     80:        const String& key=params.as_string(0, "key must be string");
                     81: 
                     82:        VMemcached& self=GET_SELF(r, VMemcached);
                     83:        self.remove(key);
                     84: }
                     85: 
                     86: MMemcached::MMemcached() : Methoded("memcached") {
1.2     ! moko       87:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 2);
        !            88:        add_native_method("flush", Method::CT_DYNAMIC, _flush, 1, 1);
        !            89:        add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.1       moko       90:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
                     91: }

E-mail: