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

1.1       moko        1: /** @file
                      2:        Parser: memcached class.
                      3: 
1.20      moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://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.22    ! moko       15: #include "pa_varray.h"
1.4       moko       16: #include "pa_vbool.h"
1.1       moko       17: #include "pa_vmemcached.h"
                     18: 
1.22    ! moko       19: volatile const char * IDENT_MEMCACHED_C="$Id: memcached.C,v 1.21 2026/10/09 21:10:38 moko Exp $";
1.1       moko       20: 
                     21: class MMemcached: public Methoded {
                     22: public: // VStateless_class
1.21      moko       23:        Value* create_new_value(Pool& apool) { return new VMemcached(apool); }
1.1       moko       24: public:
                     25:        MMemcached();
                     26: };
                     27: 
1.13      moko       28: DECLARE_CLASS_VAR(memcached, new MMemcached);
1.1       moko       29: 
                     30: static void _open(Request& r, MethodParams& params) {
1.5       moko       31:        VMemcached& self=GET_SELF(r, VMemcached);
1.3       moko       32:        Value& param_value=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE);
1.5       moko       33:        time_t ttl=params.count()>1 ? params.as_int(1, "default expiration must be int", r) : 0;
1.2       moko       34: 
1.3       moko       35:        if(HashStringValue* options=param_value.get_hash()){
1.11      moko       36:                bool connect=true;
1.3       moko       37:                String result;
                     38:                for(HashStringValue::Iterator i(*options); i; i.next()){
1.11      moko       39:                        if(i.key() == "skip-connect"){
                     40:                                connect=!i.value()->as_bool();
1.18      moko       41:                        } else if(Value *b=dynamic_cast<VBool*>(i.value())){
1.6       moko       42:                                if(b->as_bool())
                     43:                                        result << (result.is_empty() ? "--" : " --") << i.key();
                     44:                        } else {
                     45:                                const String& value=i.value()->as_string();
                     46:                                if(!value.is_empty())
1.7       moko       47:                                        result << (result.is_empty() ? "--" : " --") << i.key() << "=" << value;
1.6       moko       48:                        }
1.3       moko       49:                }
1.11      moko       50:                self.open(result, ttl, connect);
1.3       moko       51:        } else {
                     52:                const String& connect_string=params.as_string(0, "param must be connection string or options hash");
                     53:                self.open_parse(connect_string, ttl);
                     54:        }
1.2       moko       55: }
                     56: 
1.8       moko       57: static void _clear(Request& r, MethodParams& params) {
1.5       moko       58:        VMemcached& self=GET_SELF(r, VMemcached);
                     59:        time_t ttl=(params.count()>0) ? params.as_int(0, "expiration must be int", r) : 0;
1.2       moko       60: 
                     61:        self.flush(ttl);
                     62: }
                     63: 
                     64: static void _mget(Request& r, MethodParams& params) {
1.5       moko       65:        VMemcached& self=GET_SELF(r, VMemcached);
1.2       moko       66: 
1.22    ! moko       67:        ArrayString keys(params.count());
1.2       moko       68: 
1.22    ! moko       69:        for(size_t i=0; i<params.count(); i++) {
        !            70:                Value& param=params.as_no_junction(i, PARAM_MUST_NOT_BE_CODE);
        !            71:                if(const String* string=param.get_string()){
        !            72:                        keys+=string;
        !            73:                } else if(Table* table=param.get_table()){
        !            74:                        for(size_t j=0; j<table->count(); j++)
        !            75:                                keys+=table->get(j)->get(0);
        !            76:                } else if(VArray* array=dynamic_cast<VArray*>(&param)){
        !            77:                        for(ArrayValue::Iterator j(array->array()); j; j.next()){
        !            78:                                if(Value* value=j.value()){
        !            79:                                        const String* string=value->get_string();
        !            80:                                        if(!string)
        !            81:                                                value->bark("array element is '%s', it does not have string value");
        !            82:                                        keys+=string;
        !            83:                                }
        !            84:                        }
        !            85:                } else
        !            86:                        throw Exception("memcached", 0, "key must be string, table or array of strings");
        !            87:        }
1.2       moko       88: 
1.22    ! moko       89:        r.write(self.mget(keys));
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.14      moko       96:        r.write(VBool::get(self.add(key, &params.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: