v3knet/at_base — pull request #1 by andytruong (2014-01-31 04:48:52+00:00)
Title: Key-Value storage

- [ ] Code
- [ ] Test
- [ ] Documetation

```php
set($key, $value, $ttl);
at_kv($bin)->get($key);
at_kv($bin)->set(array($key_1 => $val_1, $key_2 => $val_2));
at_kv($bin)->get($key_1);
at_kv($bin)->get($key_2);

/**
* With custom engines
*
* ------------------------------------------------------
* Override
* ------------------
*
* To override default engine/otpions for specific $bin, in settings.php
* at code similiar to these lines:
*
* @file settings.php
*
* $at['kv'][$bin]['engine'] = 'db';
* $at['kv'][$bin]['options'] = array(
* 'ttl' => '+ 2 hours',
* 'database' => array(
* 'source' => 'default',
* 'target' => 'write_db',
* ),
* 'table' => 'table_with_fast_backend',
* );
*
* ------------------
* Extends
* ------------------
*
* Define new service, tagged it with 'kv', the class has to implement
* \Drupal\at_base\Helper\Key_Value_Interface.
*
* ------------------------------------------------------
*/

# ---------------------------------------------------------------
# Engine => Database (default engine)
# Default Table: at_kv
# ---------------------------------------------------------------
at_kv(
$bin,
$engine = 'db',
$options = array(
'database' => array(
'source' => 'default',
'target' => 'default',
), // which configured values from $databases['default']['default']
'table' => 'at_kv'
)
)
->set($key, $value);

# ---------------------------------------------------------------
# Engine => Disk
# Default directory: %private/kv
# ---------------------------------------------------------------
at_kv($bin, $engine = 'disk', $options = array(
'dir' => '%private/kv',
))->set($key, $value);

# ---------------------------------------------------------------
# Engine => APC
# Default prefix: substr(md5(conf_path()), 0, 7)
# ---------------------------------------------------------------
at_kv($bin, $engine = 'apc', array(
'prefix' => substr(md5(conf_path()), 0, 7),
))->set($key, $value);

# ---------------------------------------------------------------
# Engine => Memcache
# Default prefix: substr(md5(conf_path()), 0, 7)
# ---------------------------------------------------------------
at_kv($bin, $engine = 'memcache', array(
'prefix' => substr(md5(conf_path()), 0, 7),
))->set($key, $value);
```