Skip to content

Commit

Permalink
adding docs to store\mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
John Loehrer committed Mar 29, 2012
1 parent 35b0c9f commit 724d79a
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions lib/gaia/store/mysql.php
Expand Up @@ -6,19 +6,33 @@
use Gaia\Time; use Gaia\Time;
use Gaia\DB; use Gaia\DB;


// basic wrapper to make redis library conform to the cache interface. // basic wrapper to make mysql library conform to the storage interface.
// todo: figure out ways to make some of the more elegant list and member set functionality
// of redis available through the wrapper interface without breaking things.
class MySQL implements Iface { class MySQL implements Iface {


/**
* pluggable serializer
*/
protected $s; protected $s;

/**
* closure that resolves a key to a dsn/table name pair.
*/
protected $resolver; protected $resolver;


/**
* create the mysql object.
* pass in a closure to specify the dsn/tablename. receives a single key, and should return an array of:
* array( $dsn, $table );
* the dsn will be passed to Gaia\DB\Connecton::instance() to get the db object.
*/
public function __construct(\Closure $resolver, \Gaia\Serialize\Iface $s = NULL ){ public function __construct(\Closure $resolver, \Gaia\Serialize\Iface $s = NULL ){
$this->resolver = $resolver; $this->resolver = $resolver;
$this->s = ( $s ) ? $s : new \Gaia\Serialize\PHP; $this->s = ( $s ) ? $s : new \Gaia\Serialize\PHP;
} }


/**
* standard get method. wrapper for the getMulti method.
*/
public function get( $request){ public function get( $request){
if( is_array( $request ) ) return $this->getMulti( $request ); if( is_array( $request ) ) return $this->getMulti( $request );
if( ! is_scalar( $request ) ) return NULL; if( ! is_scalar( $request ) ) return NULL;
Expand All @@ -27,6 +41,10 @@ public function get( $request){
return $res[ $request ]; return $res[ $request ];
} }


/**
* easier to program for a list of keys passed in and returned, than the overloaded interface
* of the normal get method.
*/
protected function getMulti( array $request ){ protected function getMulti( array $request ){
$conns = array(); $conns = array();
foreach( $request as $k ){ foreach( $request as $k ){
Expand Down Expand Up @@ -59,6 +77,9 @@ protected function getMulti( array $request ){


} }


/**
* add a key
*/
public function add( $k, $v, $ttl = NULL ){ public function add( $k, $v, $ttl = NULL ){
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
$db = $this->db( $connstring ); $db = $this->db( $connstring );
Expand All @@ -69,7 +90,10 @@ public function add( $k, $v, $ttl = NULL ){
$rs = $db->execute("INSERT IGNORE INTO `{$table}` (`id`, `keyname`, `data`, `ttl`, `revision`) VALUES (%s, %s, %s, %i, 1)", sha1($k, TRUE), $k, $this->serialize($v), $ttl); $rs = $db->execute("INSERT IGNORE INTO `{$table}` (`id`, `keyname`, `data`, `ttl`, `revision`) VALUES (%s, %s, %s, %i, 1)", sha1($k, TRUE), $k, $this->serialize($v), $ttl);
return $rs->affected() > 0; return $rs->affected() > 0;
} }


/**
* set a key
*/
public function set( $k, $v, $ttl = NULL ){ public function set( $k, $v, $ttl = NULL ){
if( $v === NULL ) return $this->delete( $k ); if( $v === NULL ) return $this->delete( $k );
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
Expand All @@ -79,7 +103,10 @@ public function set( $k, $v, $ttl = NULL ){
$rs = $db->execute("INSERT INTO `{$table}` (`id`, `keyname`, `data`, `ttl`, `revision`) VALUES (%s, %s, %s, %i, 1) ON DUPLICATE KEY UPDATE `data` = VALUES(`data`), `ttl` = VALUES(`ttl`), `revision` = `revision` + 1", sha1($k, TRUE), $k, $this->serialize($v), $ttl); $rs = $db->execute("INSERT INTO `{$table}` (`id`, `keyname`, `data`, `ttl`, `revision`) VALUES (%s, %s, %s, %i, 1) ON DUPLICATE KEY UPDATE `data` = VALUES(`data`), `ttl` = VALUES(`ttl`), `revision` = `revision` + 1", sha1($k, TRUE), $k, $this->serialize($v), $ttl);
return $rs->affected() > 0; return $rs->affected() > 0;
} }


/**
* replace a key
*/
public function replace( $k, $v, $ttl = NULL ){ public function replace( $k, $v, $ttl = NULL ){
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
$db = $this->db( $connstring ); $db = $this->db( $connstring );
Expand All @@ -88,7 +115,10 @@ public function replace( $k, $v, $ttl = NULL ){
$rs = $db->execute("UPDATE `{$table}` SET `data` = %s, ttl = %i, `revision` = `revision` + 1 WHERE id = %s AND `ttl` >= %i", $v, $ttl, sha1($k, TRUE), $now); $rs = $db->execute("UPDATE `{$table}` SET `data` = %s, ttl = %i, `revision` = `revision` + 1 WHERE id = %s AND `ttl` >= %i", $v, $ttl, sha1($k, TRUE), $now);
return $rs->affected() > 0; return $rs->affected() > 0;
} }


/**
* replace a key
*/
public function increment( $k, $v = 1 ){ public function increment( $k, $v = 1 ){
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
$db = $this->db( $connstring ); $db = $this->db( $connstring );
Expand All @@ -99,7 +129,10 @@ public function increment( $k, $v = 1 ){
$rs->free(); $rs->free();
return $row['total']; return $row['total'];
} }


/**
* decrement a key
*/
public function decrement( $k, $v = 1 ){ public function decrement( $k, $v = 1 ){
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
$db = $this->db( $connstring ); $db = $this->db( $connstring );
Expand All @@ -110,7 +143,10 @@ public function decrement( $k, $v = 1 ){
$rs->free(); $rs->free();
return $row['total']; return $row['total'];
} }


/**
* delete a key
*/
public function delete( $k ){ public function delete( $k ){
list( $connstring, $table ) = $this->hash( $k ); list( $connstring, $table ) = $this->hash( $k );
$db = $this->db( $connstring ); $db = $this->db( $connstring );
Expand Down

0 comments on commit 724d79a

Please sign in to comment.