Esempio n. 1
0
 /**
  * Creates an index on a database with a name and an index key
  * function. The index listens to database events to update itself.
  *
  * @param $db
  * @param $name
  * @param $keyFunc
  * @param null $compare
  */
 public function __construct($db, $name, $keyFunc, $compare = null)
 {
     $this->db = $db;
     $this->name = $name;
     $this->keyFunc = $keyFunc;
     $this->compare = $compare;
     // register with database
     $this->db->on('saved', array($this, 'update'));
     $this->db->on('deleted', array($this, 'delete'));
     $this->db->on('repair', array($this, 'rebuild'));
 }
Esempio n. 2
0
 public function testCache()
 {
     $a = array();
     $f = function ($id, $data, $event = null) use(&$a) {
         if (!isset($event)) {
             $event = $data;
         }
         $a[] = $event;
     };
     self::$db->on('loaded', $f);
     $cache = new Cache(self::$db);
     self::$db->save('cached', array('foo' => 'bar'));
     $a[] = $cache->load('cached');
     $a[] = $cache->load('cached');
     $ex = array('loaded', array('foo' => 'bar'), array('foo' => 'bar'));
     $this->assertEquals($ex, $a);
 }