Flintstone\Flintstone::set PHP Method

set() public method

Set a key in the database.
public set ( string $key, mixed $data )
$key string
$data mixed
    public function set($key, $data)
    {
        $this->validateKey($key);
        $this->validateData($data);
        // If the key already exists we need to replace it
        if ($this->get($key) !== false) {
            $this->replace($key, $data);
            return;
        }
        // Write the key to the database
        $filePointer = $this->getDatabase()->openFile(Database::FILE_APPEND);
        $filePointer->fwrite($this->getLineString($key, $data));
        $this->getDatabase()->closeFile($filePointer);
        // Delete the key from cache
        if ($cache = $this->getConfig()->getCache()) {
            $cache->delete($key);
        }
    }

Usage Example

Beispiel #1
0
 protected function runOperationsTests($config)
 {
     $db = new Flintstone('test', $config);
     $arr = array('foo' => "new\nline");
     $this->assertFalse($db->get('foo'));
     $db->set('foo', 1);
     $db->set('name', 'john');
     $db->set('arr', $arr);
     $this->assertEquals(1, $db->get('foo'));
     $this->assertEquals('john', $db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $db->set('foo', 2);
     $this->assertEquals(2, $db->get('foo'));
     $this->assertEquals('john', $db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $db->delete('name');
     $this->assertFalse($db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $keys = $db->getKeys();
     $this->assertEquals(2, count($keys));
     $this->assertEquals('foo', $keys[0]);
     $this->assertEquals('arr', $keys[1]);
     $data = $db->getAll();
     $this->assertEquals(2, count($data));
     $this->assertEquals(2, $data['foo']);
     $this->assertEquals($arr, $data['arr']);
     $db->flush();
     $this->assertFalse($db->get('foo'));
     $this->assertFalse($db->get('arr'));
     $this->assertEquals(0, count($db->getKeys()));
     $this->assertEquals(0, count($db->getAll()));
     unlink($db->getDatabase()->getPath());
 }
All Usage Examples Of Flintstone\Flintstone::set