Skip to content

Commit

Permalink
Initial code checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Boyd committed Nov 4, 2011
1 parent d47a5b6 commit 86c0745
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 0 deletions.
18 changes: 18 additions & 0 deletions BerylliumCacheBundle.php
@@ -0,0 +1,18 @@
<?php

namespace Beryllium\CacheBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* BerylliumCacheBundle
*
* @uses Bundle
* @package
* @version 0.1
* @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class BerylliumCacheBundle extends Bundle
{
}
124 changes: 124 additions & 0 deletions Cache.php
@@ -0,0 +1,124 @@
<?php

namespace Beryllium\CacheBundle;

use Beryllium\CacheBundle\CacheInterface;
use Beryllium\CacheBundle\CacheClientInterface;

/**
* Cache
*
* @uses CacheInterface
* @package
* @version $id$
* @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class Cache implements CacheInterface
{
public $dic = false;

protected $client = null;
protected $safe = false;

/**
* Prep the cache
*
* @param CacheClientInterface $client Optional cache object/service
* @access public
* @return void
*/
public function __construct( CacheClientInterface $client = null )
{
if ( !empty( $client ) )
{
if ( is_object( $client ) && ( $client instanceof CacheClientInterface ) )
$this->client = $client;
else
{
throw new \Exception( 'Invalid Cache Client Interface' );
}
}
}

/**
* Inject a dependency injection container (optional)
*
* @param mixed $dic The container
* @access public
* @return void
*/
public function setContainer( $dic )
{
$this->dic = $dic;
}

/**
* Inject a cache client interface to interact with a custom cache service
*
* @param CacheClientInterface $client The client object or service
* @access public
* @return void
*/
public function setClient( CacheClientInterface $client )
{
if ( is_object( $client ) && ( $client instanceof CacheClientInterface ) )
$this->client = $client;
else
{
throw new \Exception( 'Invalid Cache Client Interface' );
}
}

/**
* Retrieve a value from the cache using the provided key
*
* @param string $key The unique key identifying the data to be retrieved.
* @access public
* @return mixed The requested data, or false if there is an error
*/
public function get( $key )
{
if ( $this->isSafe() && !empty( $key ) )
{
return $this->client->get( $key );
}

return false;
}

/**
* Add a key/value to the cache
*
* @param string $key A unique key to identify the data you want to store
* @param string $value The value you want to store in the cache
* @param int $ttl Optional: Lifetime of the data (default: 300 seconds - five minutes)
* @access public
* @return mixed Whatever the CacheClientObject returns, or false.
*/
public function set( $key, $value, $ttl = 300 )
{
if ( $this->isSafe() && !empty( $key ) )
{
return $this->client->set( $key, $value, $ttl );
}

return false;
}

/**
* Checks if the cache is in a usable state
*
* @access public
* @return boolean True if the cache is usable, otherwise false
*/
public function isSafe()
{
if ( $this->client instanceof CacheClientInterface )
{
return $this->client->isSafe();
}

return $this->safe;
}
}
42 changes: 42 additions & 0 deletions CacheClientInterface.php
@@ -0,0 +1,42 @@
<?php

namespace Beryllium\CacheBundle;

/**
* Minimum requirements for interfacing with a typical key-value store
*
* @package
* @version $id$
* @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
interface CacheClientInterface
{
/**
* Retrieve the value corresponding to a provided key
*
* @param string $key Unique identifier
* @access public
* @return mixed Result from the cache
*/
public function get( $key );

/**
* Add a value to the cache under a unique key
*
* @param string $key Unique key to identify the data
* @param mixed $value Data to store in the cache
* @param int $ttl Lifetime for stored data (in seconds)
* @access public
* @return void
*/
public function set( $key, $value, $ttl );

/**
* Check the state of the cache
*
* @access public
* @return boolean True if the cache is in a usable state, otherwise false
*/
public function isSafe();
}
21 changes: 21 additions & 0 deletions CacheInterface.php
@@ -0,0 +1,21 @@
<?php

namespace Beryllium\CacheBundle;

use Beryllium\CacheBundle\CacheClientInterface;

/**
* CacheInterface
*
* @package
* @version $id$
* @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
interface CacheInterface
{
public function __construct( CacheClientInterface $client = null );
public function get( $key );
public function set( $key, $value, $ttl );
public function isSafe();
}
128 changes: 128 additions & 0 deletions Client/FilecacheClient.php
@@ -0,0 +1,128 @@
<?php

namespace Beryllium\CacheBundle\Client;

use Beryllium\CacheBundle\ClientCacheInterface;

/**
* Completely untested and undocumented. Use at your own risk!
*
* Fixes appreciated!
*
* @uses ClientCacheInterface
* @package
* @version $id$
* @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class FilecacheClient implements ClientCacheInterface
{
protected $path = null;
public $dic = null;

public function __construct( $path = null )
{
if ( !empty( $path ) && is_dir( $path ) && is_writable( $path ) )
{
$this->path = $path;
}
else
{
$this->path = null;
}
}

public function setContainer( $dic )
{
$this->dic = $dic;
}

public function get( $key )
{
if ( !$this->isSafe() || empty( $key ) )
{
return false;
}

if ( file_exists( $this->buildFilename( $key ) )
{
$file = file_get_contents( $this->buildFilename( $key ) );
$file = unserialize( $file );

if ( !is_array( $file ) )
{
return false;
}
else if ( $file[ 'key' ] != $key )
{
return false;
}
else if ( time() - $file[ 'ctime' ] > $file[ 'ttl' ] )
{
return false;
}
else
{
return unserialize( $file[ 'value' ] );
}
}
else
{
return false;
}
}

public function set( $key, $value, $ttl = 300 )
{
$file = array();
$file[ 'key' ] = $key;

$file[ 'value' ] = serialize( $value );

$file[ 'ttl' ] = $ttl;
$file[ 'ctime' ] = time();

if ( $this->isSafe() && !empty( $key ) )
{
return file_put_contents( $this->buildFilename( $key ), serialize( $file ) );
}
else
{
return false;
}
}

public function setPath( $path )
{
if ( !empty( $path ) && is_dir( $path ) && is_writable( $path ) )
{
$this->path = $path;
return true;
}
else
{
$this->path = null;
return false;
}
}

public function isSafe()
{
if ( is_null( $this->path ) )
{
return false;
}

return is_dir( $this->path ) && is_writable( $this->path );
}

public function isFull()
{
//Check if the cache has exceeded its alotted size
}

protected function buildFilename( $key )
{
return $this->path . md5( $key ) . '_file.cache';
}
}

0 comments on commit 86c0745

Please sign in to comment.