PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/mahimarathore/mahi
PHP | 142 lines | 94 code | 14 blank | 34 comment | 14 complexity | a4f60c796a5704db853534d394695706 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Undocumented
  4. *
  5. * @link http://agiletoolkit.org/
  6. *//*
  7. ==ATK4===================================================
  8. This file is part of Agile Toolkit 4
  9. http://agiletoolkit.org/
  10. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  11. Distributed under Affero General Public License v3 and
  12. commercial license.
  13. See LICENSE or LICENSE_COM for more information
  14. =====================================================ATK4=*/
  15. /*
  16. * Implementation of Memcached data controller
  17. *
  18. * $m=$this->add('Model');
  19. * $m->addField('test');
  20. * $m->table='test_table';
  21. *
  22. * $m->setSource('Memcached', $storage);
  23. * $m['test']=123;
  24. * $m->save(1);
  25. *
  26. * $m['test']=321;
  27. * $m->save(2);
  28. *
  29. * $m->load(1);
  30. * echo $m['test'];
  31. */
  32. class Controller_Data_Memcached extends Controller_Data {
  33. function setSource($model,$data=undefined){
  34. parent::setSource($model,array(
  35. 'db'=>new Memcached($x=$data.'_'.$model->table),
  36. 'prefix'=>$model->table
  37. ));
  38. if(!$model->_table[$this->short_name]['db']->getServerList()){
  39. $model->_table[$this->short_name]['db']->addServer('localhost', 11211);
  40. }
  41. if(!$model->hasElement($model->id_field))$model->addField($model->id_field)->system(true);
  42. return $this;
  43. }
  44. function save($model,$id=null){
  45. if(is_null($model->id)){
  46. $id=uniqid();
  47. if($model->id_field){
  48. $model->data[$model->id_field]=$id;
  49. }
  50. }
  51. $model->_table[$this->short_name]['db']->set($id,$model->data);
  52. return $id;
  53. }
  54. function tryLoad($model,$id){
  55. $model->data=$model->_table[$this->short_name]['db']->get($id);
  56. if($model->data===false){
  57. return $this; // not loaded
  58. }
  59. $model->dirty=array();
  60. $model->id=$id;
  61. if($model->id_field){
  62. $model->data[$model->id_field]=$id;
  63. }
  64. return $this;
  65. }
  66. function load($model,$id=null){
  67. $this->tryLoad($model,$id);
  68. if(!$model->loaded())throw $this->exception('Unable to load data')
  69. ->addMoreInfo('id',$id);
  70. return $this;
  71. }
  72. function getBy($model,$field,$cond=undefined,$value=undefined){
  73. $data=$model->_table[$this->short_name]['db']->get($id);
  74. if($data===false)throw $this->exception('Unable to load data');
  75. return $data;
  76. }
  77. function tryLoadBy($model,$field,$cond=undefined,$value=undefined){
  78. throw $this->exception('tryLoadBy is not supported by Memcached');
  79. }
  80. function tryLoadAny($model){
  81. throw $this->exception('tryLoadAny is not supported by Memcached');
  82. }
  83. function loadBy($model,$field,$cond=undefined,$value=undefined){
  84. throw $this->exception('loadBy is not supported by Memcached');
  85. }
  86. function delete($model,$id=null){
  87. $model->_table[$this->short_name]['db']->delete($id?:$model->id);
  88. if($model->id==$id || is_null($id))
  89. $model->unload();
  90. }
  91. function deleteAll($model){
  92. $model->_table[$this->short_name]['db']->flush();
  93. }
  94. function getRows($model){
  95. return $model->_table;
  96. $t =& $model->_table[$this->short_name];
  97. }
  98. function setOrder($model,$field,$desc=false){
  99. // TODO: sort array
  100. }
  101. function setLimit($model,$count,$offset=0){
  102. // TODO: splice
  103. }
  104. function rewind($model){
  105. reset($model->_table[$this->short_name]);
  106. list($model->id,$model->data)=each($model->_table[$this->short_name]);
  107. if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
  108. return $model->data;
  109. }
  110. function next($model){
  111. list($model->id,$model->data)=each($model->_table[$this->short_name]);
  112. if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
  113. $model->set("id", $model->id); // romans, revise please - otherwise, array based source not working properly
  114. return $model;
  115. }
  116. function setAssoc($data){
  117. $this->array_data=array();
  118. foreach($data as $id=>$name){
  119. $this->array_data[]=array('id'=>$id,'name'=>$name);
  120. }
  121. return $this;
  122. }
  123. function getActualFields(){
  124. return array();
  125. }
  126. }