Skip to content

Commit

Permalink
Interface improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 20, 2013
1 parent 4090946 commit 7098429
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
27 changes: 21 additions & 6 deletions README.md
@@ -1,15 +1,30 @@
CacheKit
========

CacheKit interface

$c = new CacheKit;
$memory = $c->createBackend( 'MemoryCache' );
CacheKit Interface

$c->set( 'foo' , 123 );
```php
$c = new CacheKit;
$memory = $c->createBackend( 'MemoryCache' );

$memory->set( 'foo' , '123' );
$memory->get('foo');
$c->set( 'foo' , 123 );

$memory->set( 'foo' , '123' );
$val = $memory->get('foo');
```

ApcCache Interface

```php
$cache = new CacheKit\ApcCache(array(
'namespace' => 'app_',
'default_expiry' => 3600,
));
$cache->set($name,$val);
$val = $cache->get($name);
$cache->remove($name);
```


FileSystemCache
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Expand Up @@ -3,6 +3,7 @@

<phpunit bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
verbose="true">
<testsuites>
<testsuite name="PHPUnit">
Expand Down
26 changes: 19 additions & 7 deletions src/CacheKit/ApcCache.php
Expand Up @@ -17,36 +17,48 @@ class ApcCache

public $defaultExpiry = 0;

function __construct( $options = array() )
public function __construct( $options = array() )
{
if( isset($options['namespace']) )
if ( isset($options['namespace']) ) {
$this->namespace = $options['namespace'];
if( isset($options['default_expiry'] ) )
}
if ( isset($options['default_expiry'] ) ) {
$this->defaultExpiry = $options['default_expiry'];
}
}

function get($key)
public function get($key)
{
return apc_fetch( $this->namespace . ':' . $key );
}

function set($key,$value,$ttl = null)
public function set($key,$value,$ttl = null)
{
if( null === $ttl && $this->defaultExpiry )
$ttl = $this->defaultExpiry;
apc_store( $this->namespace . ':' . $key , $value , $ttl );
}

function remove($key)
public function remove($key)
{
apc_delete( $this->namespace . ':' . $key );
}

function clear()
public function clear()
{
apc_clear_cache();
}

public function __get($name)
{
return $this->get($name);
}

public function __set($name,$value)
{
$this->set($name,$value);
}

static function getInstance()
{
static $instance;
Expand Down
8 changes: 4 additions & 4 deletions src/CacheKit/CacheInterface.php
Expand Up @@ -12,9 +12,9 @@

interface CacheInterface
{
function get($key);
function set($key,$value,$ttl = 0);
function remove($key);
function clear();
public function get($key);
public function set($key,$value,$ttl = 0);
public function remove($key);
public function clear();
}

11 changes: 11 additions & 0 deletions src/CacheKit/FileSystemCache.php
Expand Up @@ -38,6 +38,7 @@ public function __construct($options = array() )
};
}


public function _getCacheFilepath($key)
{
$filename = call_user_func($this->filenameBuilder,$key);
Expand All @@ -62,6 +63,16 @@ public function _encodeFile($file,$data)
return file_put_contents( $file, $content );
}

public function __get($key)
{
return $this->get($key);
}

public function __set($key,$val)
{
return $this->set($key,$val);
}

public function get($key)
{
$path = $this->_getCacheFilepath($key);
Expand Down
23 changes: 17 additions & 6 deletions src/CacheKit/MemcacheCache.php
Expand Up @@ -18,7 +18,7 @@ class MemcacheCache
private $handle;
public $compress = false;

function __construct($servers = array() )
public function __construct($servers = array() )
{
$this->handle = new Memcache;
foreach( $servers as $server ) {
Expand All @@ -27,24 +27,35 @@ function __construct($servers = array() )
}
}

function set($key,$value,$ttl = 0)
public function __get($key)
{
return $this->get($key);
}

public function __set($key,$val)
{
return $this->set($key,$val);
}

public function set($key,$value,$ttl = 0)
{
$this->handle->set( $key , serialize( $value ) , $this->compress , $ttl );
}

function get($key)
public function get($key)
{
$v = $this->handle->get( $key );
if( $v )
if ( $v ) {
return unserialize($v);
}
}

function remove($key)
public function remove($key)
{
$this->handle->delete($key);
}

function clear()
public function clear()
{
$this->handle->flush();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CacheKit/FileSystemCacheTest.php
Expand Up @@ -29,15 +29,15 @@ function testSerializer()
ok( null === $cache->get( $url ) );
}

function test()
public function test()
{
$cache = new CacheKit\FileSystemCache(array(
'expiry' => 30,
'cache_dir' => 'cache',
));

$url = 'http://google.com';
$cache->set( $url , $html = file_get_contents($url) );
$cache->set( $url , $html = 'blah' );
$html2 = $cache->get( $url );

is( $html , $html2 );
Expand Down

0 comments on commit 7098429

Please sign in to comment.