EMongoCacheDependency.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * EMongoCacheDependency represents a dependency based on the query result of a Mongo Query.
  4. *
  5. * If the query result (a scalar) changes, the dependency is considered as changed.
  6. * To specify the Mongo Cursor, set {@link cursor} property.
  7. */
  8. class EMongoCacheDependency extends CCacheDependency
  9. {
  10. /**
  11. * @var string the ID of a {@link EMongoClient} application component. Defaults to 'mongodb'.
  12. */
  13. public $connectionID = 'mongodb';
  14. public $collection = null;
  15. public $query = array();
  16. private $_db;
  17. /**
  18. * Constructor.
  19. * @param string $cursor the Mongo Cursor whose result is used to determine if the dependency has been changed.
  20. */
  21. public function __construct($collection=null, $query = null)
  22. {
  23. $this->collection = $collection;
  24. $this->query = $query;
  25. }
  26. /**
  27. * PHP sleep magic method.
  28. * This method ensures that the database instance is set null because it contains resource handles.
  29. * @return array
  30. */
  31. public function __sleep()
  32. {
  33. $this->_db = null;
  34. return array_keys((array)$this);
  35. }
  36. /**
  37. * Generates the data needed to determine if dependency has been changed.
  38. * This method returns the value of the global state.
  39. * @throws CException if {@link cursor} is empty
  40. * @return mixed the data needed to determine if dependency has been changed.
  41. */
  42. protected function generateDependentData()
  43. {
  44. if($this->query !== null){
  45. $db = $this->getDbConnection();
  46. if($db->queryCachingDuration > 0){
  47. // temporarily disable and re-enable query caching
  48. $duration=$db->queryCachingDuration;
  49. $db->queryCachingDuration = 0;
  50. $result = iterator_to_array($this->createCursor());
  51. $db->queryCachingDuration = $duration;
  52. }else{
  53. $result = iterator_to_array($this->createCursor());
  54. }
  55. return $result;
  56. }else{
  57. throw new EMongoException(Yii::t('yii','EMongoCacheDependency.query cannot be empty.'));
  58. }
  59. }
  60. protected function createCursor()
  61. {
  62. $query = array();
  63. if(isset($this->query[0])){
  64. $query = $this->query[0];
  65. }
  66. if (empty($this->collection)) {
  67. throw new EMongoException(Yii::t('yii','EMongoCacheDependency.collection cannot be empty.'));
  68. }
  69. $cursor = $this->getDbConnection()->{$this->collection}->find($query);
  70. if(isset($this->query['sort'])){
  71. $cursor->sort($this->query['sort']);
  72. }
  73. if(isset($this->query['skip'])){
  74. $cursor->limit($this->query['skip']);
  75. }
  76. if(isset($this->query['limit'])){
  77. $cursor->limit($this->query['limit']);
  78. }
  79. return $cursor;
  80. }
  81. /**
  82. * @return CDbConnection the DB connection instance
  83. * @throws CException if {@link connectionID} does not point to a valid application component.
  84. */
  85. protected function getDbConnection()
  86. {
  87. if($this->_db!==null){
  88. return $this->_db;
  89. }else{
  90. if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof EMongoClient){
  91. return $this->_db;
  92. }else{
  93. throw new EMongoException(
  94. Yii::t(
  95. 'yii',
  96. 'EMongoCacheDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a EMongoClient application component.',
  97. array('{id}' => $this->connectionID)
  98. )
  99. );
  100. }
  101. }
  102. }
  103. }