Skip to content

Commit

Permalink
Fix stupid folder error.
Browse files Browse the repository at this point in the history
  • Loading branch information
vegardlarsen committed May 13, 2014
1 parent ca9cb03 commit f517bde
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 142 deletions.
214 changes: 107 additions & 107 deletions src/src/Cache.php → src/Cache.php
Original file line number Diff line number Diff line change
@@ -1,108 +1,108 @@
<?php

namespace DC\Cache\Implementations\Memcache;

class Cache implements \DC\Cache\ICache {

/**
* @var \Memcache
*/
private $memcache;
private $isConnected = false;
/**
* @var MemcacheConfiguration
*/
private $configuration;

function __construct(MemcacheConfiguration $configuration) {
$this->memcache = new \Memcache;
$this->configuration = $configuration;
}

/**
* Only connect at the last possible moment. This enables you to get the cache provider, but not connect
* until you have business with the cache.
*/
private function connect() {
if ($this->isConnected) return;
$this->memcache->connect($this->configuration->getHost(), $this->configuration->getPort());
}

/**
* Retrieve an item from cache
*
* @param string $key The key to store it under
* @return mixed
*/
function get($key)
{
$this->connect();
return $this->memcache->get($key);
}

/**
* Set an item in the cache
*
* @param string $key
* @param mixed $value
* @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int)
* @return void
*/
function set($key, $value, $validity = null)
{
$this->connect();
$expires = 0;
if ($validity instanceof \DateInterval) {
$expires = date_create('@0')->add($validity)->getTimestamp();
} else if ($validity instanceof \DateTime) {
$expires = $validity->getTimestamp() - time();
} else if (is_numeric($validity)) {
$expires = (int)$validity;
}

$this->memcache->add($key, $value, null, $expires);
}

/**
* Try to get an item, and if missed call the fallback method to produce the value and store it.
*
* @param string $key
* @param callable $fallback
* @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int)
* @return mixed
*/
function getWithFallback($key, callable $fallback, $validity = null)
{
$value = $this->get($key);
// TODO: find a better way to prevent false negatives (boolean false as the stored value), e.g. by manually
// serializing everything that goes in, and having get() throw if the content is not serialized
if ($value === false) {
$value = $fallback();
$this->set($key, $value, $validity);
}
return $value;
}

/**
* Remove a key from the cache.
*
* @param string $key
* @return void
*/
function delete($key)
{
$this->connect();
$this->memcache->delete($key);
}

/**
* Remove all items from the cache (flush it).
*
* @return void
*/
function deleteAll()
{
$this->connect();
$this->memcache->flush();
}
<?php

namespace DC\Cache\Implementations\Memcache;

class Cache implements \DC\Cache\ICache {

/**
* @var \Memcache
*/
private $memcache;
private $isConnected = false;
/**
* @var MemcacheConfiguration
*/
private $configuration;

function __construct(MemcacheConfiguration $configuration) {
$this->memcache = new \Memcache;
$this->configuration = $configuration;
}

/**
* Only connect at the last possible moment. This enables you to get the cache provider, but not connect
* until you have business with the cache.
*/
private function connect() {
if ($this->isConnected) return;
$this->memcache->connect($this->configuration->getHost(), $this->configuration->getPort());
}

/**
* Retrieve an item from cache
*
* @param string $key The key to store it under
* @return mixed
*/
function get($key)
{
$this->connect();
return $this->memcache->get($key);
}

/**
* Set an item in the cache
*
* @param string $key
* @param mixed $value
* @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int)
* @return void
*/
function set($key, $value, $validity = null)
{
$this->connect();
$expires = 0;
if ($validity instanceof \DateInterval) {
$expires = date_create('@0')->add($validity)->getTimestamp();
} else if ($validity instanceof \DateTime) {
$expires = $validity->getTimestamp() - time();
} else if (is_numeric($validity)) {
$expires = (int)$validity;
}

$this->memcache->add($key, $value, null, $expires);
}

/**
* Try to get an item, and if missed call the fallback method to produce the value and store it.
*
* @param string $key
* @param callable $fallback
* @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int)
* @return mixed
*/
function getWithFallback($key, callable $fallback, $validity = null)
{
$value = $this->get($key);
// TODO: find a better way to prevent false negatives (boolean false as the stored value), e.g. by manually
// serializing everything that goes in, and having get() throw if the content is not serialized
if ($value === false) {
$value = $fallback();
$this->set($key, $value, $validity);
}
return $value;
}

/**
* Remove a key from the cache.
*
* @param string $key
* @return void
*/
function delete($key)
{
$this->connect();
$this->memcache->delete($key);
}

/**
* Remove all items from the cache (flush it).
*
* @return void
*/
function deleteAll()
{
$this->connect();
$this->memcache->flush();
}
}
70 changes: 35 additions & 35 deletions src/src/MemcacheConfiguration.php → src/MemcacheConfiguration.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?php

namespace DC\Cache\Implementations\Memcache;

class MemcacheConfiguration {
/**
* @var string
*/
private $host;
/**
* @var int
*/
private $port;

function __construct($host, $port = null) {

$this->host = $host;
$this->port = $port;
}

/**
* @return string
*/
public function getHost()
{
return $this->host;
}

/**
* @return int
*/
public function getPort()
{
return $this->port;
}
<?php

namespace DC\Cache\Implementations\Memcache;

class MemcacheConfiguration {
/**
* @var string
*/
private $host;
/**
* @var int
*/
private $port;

function __construct($host, $port = null) {

$this->host = $host;
$this->port = $port;
}

/**
* @return string
*/
public function getHost()
{
return $this->host;
}

/**
* @return int
*/
public function getPort()
{
return $this->port;
}
}

0 comments on commit f517bde

Please sign in to comment.