mahi /atk4/lib/Controller/Data/Memcached.php

Language PHP Lines 143
MD5 Hash a4f60c796a5704db853534d394695706
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php // vim:ts=4:sw=4:et:fdm=marker
/*
 * Undocumented
 *
 * @link http://agiletoolkit.org/
*//*
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
/*
 * Implementation of Memcached data controller
 *
 * $m=$this->add('Model');
 * $m->addField('test');
 * $m->table='test_table';
 *
 * $m->setSource('Memcached', $storage);
 * $m['test']=123;
 * $m->save(1);
 *
 * $m['test']=321;
 * $m->save(2);
 *
 * $m->load(1);
 * echo $m['test'];
 */

class Controller_Data_Memcached extends Controller_Data {

    function setSource($model,$data=undefined){
        parent::setSource($model,array(
            'db'=>new Memcached($x=$data.'_'.$model->table),
            'prefix'=>$model->table
        ));

        if(!$model->_table[$this->short_name]['db']->getServerList()){
            $model->_table[$this->short_name]['db']->addServer('localhost', 11211);
        }

        if(!$model->hasElement($model->id_field))$model->addField($model->id_field)->system(true);

        return $this;
    }

    function save($model,$id=null){

        if(is_null($model->id)){
            $id=uniqid();
            if($model->id_field){
                $model->data[$model->id_field]=$id;
            }
        }
        $model->_table[$this->short_name]['db']->set($id,$model->data);
        return $id;
    }
    function tryLoad($model,$id){
        $model->data=$model->_table[$this->short_name]['db']->get($id);
        if($model->data===false){
            return $this; // not loaded
        }
        $model->dirty=array();

        $model->id=$id;
        if($model->id_field){
            $model->data[$model->id_field]=$id;
        }

        return $this;
    }
    function load($model,$id=null){
        $this->tryLoad($model,$id);
        if(!$model->loaded())throw $this->exception('Unable to load data')
            ->addMoreInfo('id',$id);
        return $this;
    }


    function getBy($model,$field,$cond=undefined,$value=undefined){
        $data=$model->_table[$this->short_name]['db']->get($id);
        if($data===false)throw $this->exception('Unable to load data');
        return $data;
    }
    function tryLoadBy($model,$field,$cond=undefined,$value=undefined){
        throw $this->exception('tryLoadBy is not supported by Memcached');
    }
    function tryLoadAny($model){
        throw $this->exception('tryLoadAny is not supported by Memcached');
    }
    function loadBy($model,$field,$cond=undefined,$value=undefined){
        throw $this->exception('loadBy is not supported by Memcached');
    }
    function delete($model,$id=null){
        $model->_table[$this->short_name]['db']->delete($id?:$model->id);
        if($model->id==$id || is_null($id))
            $model->unload();
    }

    function deleteAll($model){
        $model->_table[$this->short_name]['db']->flush();
    }
    function getRows($model){
        return $model->_table;
        $t =& $model->_table[$this->short_name];
    }
    function setOrder($model,$field,$desc=false){
        // TODO: sort array
    }
    function setLimit($model,$count,$offset=0){
        // TODO: splice
    }

    function rewind($model){
        reset($model->_table[$this->short_name]);

        list($model->id,$model->data)=each($model->_table[$this->short_name]);
        if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
        return $model->data;
    }
    function next($model){
        list($model->id,$model->data)=each($model->_table[$this->short_name]);
        if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
        $model->set("id", $model->id); // romans, revise please - otherwise, array based source not working properly
        return $model;
    }
    function setAssoc($data){
        $this->array_data=array();
        foreach($data as $id=>$name){
            $this->array_data[]=array('id'=>$id,'name'=>$name);
        }
        return $this;
    }
    function getActualFields(){
        return array();
    }
}
Back to Top