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

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.10    ! moko       18: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.9 2013/04/23 22:57:06 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()){
1.6       moko       37:                        if(Value *b=i.value()->as("bool")){
                     38:                                if(b->as_bool())
                     39:                                        result << (result.is_empty() ? "--" : " --") << i.key();
                     40:                        } else {
                     41:                                const String& value=i.value()->as_string();
                     42:                                if(!value.is_empty())
1.7       moko       43:                                        result << (result.is_empty() ? "--" : " --") << i.key() << "=" << value;
1.6       moko       44:                        }
1.3       moko       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: 
1.8       moko       53: static void _clear(Request& r, MethodParams& params) {
1.5       moko       54:        VMemcached& self=GET_SELF(r, VMemcached);
                     55:        time_t ttl=(params.count()>0) ? params.as_int(0, "expiration must be int", r) : 0;
1.2       moko       56: 
                     57:        self.flush(ttl);
                     58: }
                     59: 
                     60: static void _mget(Request& r, MethodParams& params) {
1.5       moko       61:        VMemcached& self=GET_SELF(r, VMemcached);
1.2       moko       62:        Value& param=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
                     63: 
                     64:        if(param.is_string()){
                     65: 
                     66:                ArrayString keys(params.count());
                     67:                
                     68:                for(size_t i=0; i<params.count(); i++) {
                     69:                        keys+=&params.as_string(i, "key must be string");
                     70:                }
                     71: 
                     72:                r.write_no_lang(self.mget(keys));
                     73:        } else {
                     74:                Table* table=param.get_table();
                     75:                if(table==0){
                     76:                        throw Exception("memcached", 0, "key must be string or table");
                     77:                }
                     78:                
                     79:                ArrayString keys(table->count());
                     80:                
                     81:                for(size_t i=0; i<table->count(); i++) {
                     82:                        keys+=table->get(i)->get(0);
                     83:                }
                     84: 
                     85:                r.write_no_lang(self.mget(keys));
                     86:        }
1.1       moko       87: }
                     88: 
1.4       moko       89: static void _add(Request& r, MethodParams& params) {
1.5       moko       90:        VMemcached& self=GET_SELF(r, VMemcached);
1.4       moko       91:        const String& key=params.as_string(0, "key must be string");
                     92: 
1.5       moko       93:        r.write_no_lang(VBool::get(self.add(key, &params.as_no_junction(1, PARAM_MUST_NOT_BE_CODE))));
1.4       moko       94: }
                     95: 
1.1       moko       96: static void _delete(Request& r, MethodParams& params) {
1.5       moko       97:        VMemcached& self=GET_SELF(r, VMemcached);
1.1       moko       98:        const String& key=params.as_string(0, "key must be string");
                     99: 
                    100:        self.remove(key);
                    101: }
                    102: 
1.10    ! moko      103: static void _release(Request& r, MethodParams&) {
1.9       moko      104:        VMemcached& self=GET_SELF(r, VMemcached);
                    105: 
                    106:        self.quit();
                    107: }
                    108: 
1.1       moko      109: MMemcached::MMemcached() : Methoded("memcached") {
1.2       moko      110:        add_native_method("open", Method::CT_DYNAMIC, _open, 1, 2);
1.8       moko      111:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 1);
1.2       moko      112:        add_native_method("mget", Method::CT_DYNAMIC, _mget, 1, 1000);
1.4       moko      113:        add_native_method("add", Method::CT_DYNAMIC, _add, 2, 2);
1.1       moko      114:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.9       moko      115:        add_native_method("release", Method::CT_DYNAMIC, _release, 0, 0);
1.1       moko      116: }

E-mail: