/** * Store an item in the cache for a given number of minutes. * * @param string $key * @param mixed $value * @param int $minutes * @return void */ public function put($key, $value, $minutes) { // All of the cached values in the database are encrypted in case this is used // as a session data store by the consumer. We'll also calculate the expire // time and place that on the table so we will check it on our retrieval. $value = $this->encrypter->encrypt($value); $timestamp = $this->getTime(); $expiration = $ttl = $timestamp + $minutes * 60; // Remove key/value store if exists $this->forget($key); $this->columnFamily->insert($this->prefix . $key, compact('value', 'expiration'), $timestamp, $ttl); }
public function test_serialized_col_name_validators() { $cf = new ColumnFamily($this->client, 'CompositeNames'); $columns = array(serialize(array(0, 'meta')) => 'foo', serialize(array(1, 'blah')) => 10); $cf->insert('key', $columns); $this->assertEquals($columns, $cf->get('key')); }
protected function setValue($key, $value, $expire = 0) { $this->cache[$key] = $value; $cf = new ColumnFamily($this->cassandra, $this->config['column_family']); $str = json_encode($value); if ($expire > 0) { try { $seconds = $expire - time(); // __data key set as C* requires a field, note: max TTL can only be 630720000 seconds $cf->insert($key, array('__data' => $str), null, $seconds); } catch (\Exception $e) { return false; } } else { try { // __data key set as C* requires a field $cf->insert($key, array('__data' => $str)); } catch (\Exception $e) { return false; } } return true; }
require_once __DIR__ . '/../lib/autoload.php'; use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; use phpcassa\ColumnSlice; use phpcassa\SystemManager; use phpcassa\Schema\StrategyClass; // Create a new keyspace and column family $sys = new SystemManager('127.0.0.1'); $sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1'))); $sys->create_column_family('Keyspace1', 'Letters', array("column_type" => "Standard", "comparator_type" => "LongType", "key_validation_class" => "UTF8Type", "default_validation_class" => "UTF8Type")); // Start a connection pool, create our ColumnFamily instance $pool = new ConnectionPool('Keyspace1', array('127.0.0.1')); $letters = new ColumnFamily($pool, 'Letters'); // Insert a few records $columns = array(1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e"); $letters->insert('key', $columns); function print_slice($columns) { echo "("; foreach ($columns as $number => $letter) { echo "{$number} => {$letter}, "; } echo ")\n"; } // Fetch everything >= 2 $slice = new ColumnSlice(2); print_slice($letters->get('key', $slice)); // Fetch everything between 2 and 4, inclusive $slice = new ColumnSlice(2, 4); print_slice($letters->get('key', $slice)); // Fetch the first three columns in the row
// Create a new keyspace and column family $sys = new SystemManager('127.0.0.1'); $sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1'))); // Use composites for column names and row keys $sys->create_column_family('Keyspace1', 'Composites', array("comparator_type" => "CompositeType(LongType, AsciiType)", "key_validation_class" => "CompositeType(AsciiType, LongType)")); // Start a connection pool, create our ColumnFamily instance $pool = new ConnectionPool('Keyspace1', array('127.0.0.1')); $cf = new ColumnFamily($pool, 'Composites'); // Make it easier to work with non-scalar types $cf->insert_format = ColumnFamily::ARRAY_FORMAT; $cf->return_format = ColumnFamily::ARRAY_FORMAT; // Insert a few records $key1 = array("key", 1); $key2 = array("key", 2); $columns = array(array(array(0, "a"), "val0a"), array(array(1, "a"), "val1a"), array(array(1, "b"), "val1b"), array(array(1, "c"), "val1c"), array(array(2, "a"), "val2a"), array(array(3, "a"), "val3a")); $cf->insert($key1, $columns); $cf->insert($key2, $columns); // Fetch a user record $row = $cf->get($key1); $col1 = $row[0]; list($name, $value) = $col1; echo "Column name: "; print_r($name); echo "Column value: "; print_r($value); echo "\n\n"; // Fetch columns with a first component of 1 $slice = new ColumnSlice(array(1), array(1)); $columns = $cf->get($key1, $slice); foreach ($columns as $column) { list($name, $value) = $column;
public function test_initial_connection_failure() { $servers = array('localhost', 'foobar'); $pool = new SilentConnectionPool(self::$KS, $servers); $pool->fill(); $stats = $pool->stats(); $this->assertEquals($stats['created'], 5); $this->assertTrue($stats['failed'] == 5 || $stats['failed'] == 4); $cf = new ColumnFamily($pool, self::$CF); foreach (range(1, 50) as $i) { $cf->insert('key', array('c' => 'v')); } $pool->dispose(); $servers = array('barfoo', 'foobar'); $pool = new SilentConnectionPool(self::$KS, $servers); $this->setExpectedException('\\phpcassa\\Connection\\NoServerAvailable'); $pool->fill(); }
* */ require_once __DIR__ . '/../lib/autoload.php'; use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; use phpcassa\SystemManager; use phpcassa\Schema\StrategyClass; // Create a new keyspace and column family $sys = new SystemManager('127.0.0.1'); $sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1'))); $sys->create_column_family('Keyspace1', 'Users'); // Start a connection pool, create our ColumnFamily instance $pool = new ConnectionPool('Keyspace1', array('127.0.0.1')); $users = new ColumnFamily($pool, 'Users'); // Insert a few records $users->insert('user0', array("name" => "joe", "state" => "TX")); $users->insert('user1', array("name" => "bob", "state" => "CA")); // Fetch a user record $user = $users->get('user0'); $name = $user["name"]; echo "Fetched user {$name}\n"; // Fetch both at once $both = $users->multiget(array('user0', 'user1')); foreach ($both as $user_id => $columns) { echo "{$user_id} state: " . $columns["state"] . "\n"; } // Only fetch the name of user1 $columns = $users->get('user1', $column_slice = null, $column_names = array("name")); echo "Name is " . $columns["name"] . "\n"; // Insert two more records at once $users->batch_insert(array("user3" => array("name" => "kat"), "user4" => array("name" => "tom")));
public function test_get_indexed_slices() { $this->require_opp(); $indexed_cf = new ColumnFamily($this->pool, 'Indexed1'); $indexed_cf->truncate(); $columns = array('birthdate' => 1); foreach (range(1, 3) as $i) { $indexed_cf->insert('key' . $i, $columns); } $expr = new IndexExpression($column_name = 'birthdate', $value = 1); $clause = new IndexClause(array($expr), 10000); $result = $indexed_cf->get_indexed_slices($clause); $count = 0; foreach ($result as $key => $cols) { $count++; $this->assertEquals($columns, $cols); $this->assertEquals($key, "key{$count}"); } $this->assertEquals($count, 3); # Insert and remove a matching row at the beginning $indexed_cf->insert('key0', $columns); $indexed_cf->remove('key0'); # Insert and remove a matching row at the end $indexed_cf->insert('key4', $columns); $indexed_cf->remove('key4'); # Remove a matching row from the middle $indexed_cf->remove('key2'); $result = $indexed_cf->get_indexed_slices($clause); $count = 0; foreach ($result as $key => $cols) { $count++; $this->assertContains($key, array("key1", "key3")); } $this->assertEquals($count, 2); $indexed_cf->truncate(); $keys = array(); foreach (range(1, 1000) as $i) { $indexed_cf->insert("key{$i}", $columns); if ($i % 50 != 0) { $indexed_cf->remove("key{$i}"); } else { $keys[] = "key{$i}"; } } $count = 0; foreach ($result as $key => $cols) { $count++; $this->assertContains($key, $keys); unset($keys[$key]); } $this->assertEquals($count, 20); $indexed_cf->truncate(); }
// Start a connection pool, create our ColumnFamily instance $pool = new ConnectionPool('Keyspace1', array('127.0.0.1')); $count_cf = new ColumnFamily($pool, 'Counts'); // ColumnFamily::add() is the easiest way to increment a counter $count_cf->add("key1", "col1"); $results = $count_cf->get("key1"); $current_count = $results["col1"]; echo "After one add(): {$current_count}\n"; // You can specify a value other than 1 to adjust the counter by $count_cf->add("key1", "col1", 10); $results = $count_cf->get("key1"); $current_count = $results["col1"]; echo "After add(10): {$current_count}\n"; // ColumnFamily::insert() will also increment values, but you can // adjust multiple columns at the same time $count_cf->insert("key1", array("col1" => 3, "col2" => -1)); $results = $count_cf->get("key1"); $current_count = $results["col1"]; echo "After insert(3): {$current_count}\n\n"; // And ColumnFamily::batch_insert() lets you increment counters // in multiple rows at the same time $count_cf->batch_insert(array("key1" => array("col1" => 1, "col2" => -1), "key2" => array("col1" => 16))); echo "After batch_insert:\n"; print_r($count_cf->multiget(array("key1", "key2"))); // ColumnFamily::remove_counter() should basically only be used when you // will *never* use a counter again $count_cf->remove_counter("key1", "col1"); echo "\nRow key1 after remove_counter(key1, col1):\n"; print_r($count_cf->get("key1")); // Destroy our schema $sys->drop_keyspace("Keyspace1");
} else { $data[$column_key_name][$column_name] = $column_value; } } $no_column++; } $no_scf++; } try { if (!empty($key)) { if (count($data) > 0) { if (isset($_POST['mode']) && $_POST['mode'] == 'edit') { $column_family->remove($key); } $time_start = microtime(true); $column_family->insert($key, $data); $time_end = microtime(true); // Insert successful if (isset($_POST['mode']) && $_POST['mode'] == 'insert') { $vw_vars['success_message'] = displaySuccessMessage('insert_row', array('query_time' => getQueryTime($time_start, $time_end))); } else { $vw_vars['success_message'] = displaySuccessMessage('edit_row', array('key' => $key, 'query_time' => getQueryTime($time_start, $time_end))); } } else { $vw_vars['error_message'] = displayErrorMessage('insert_row_incomplete_fields'); } } else { $vw_vars['info_message'] = displayInfoMessage('insert_row_not_empty'); } } catch (Exception $e) { $vw_vars['error_message'] = displayErrormessage('insert_row', array('message' => $e->getMessage()));