Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/454d5e26b58eee7ad6d7 to your computer and use it in GitHub Desktop.
Save doctrinebot/454d5e26b58eee7ad6d7 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-48 - https://github.com/doctrine/doctrine2/issues/4983
Index: Common/Cache/XcacheCache.php
===================================================================
--- Common/Cache/XcacheCache.php (revision 6522)
+++ Common/Cache/XcacheCache.php (working copy)
@@ -34,12 +34,17 @@
*/
class XcacheCache implements Cache
{
+ /**
+ * @var namespace
+ */
+ private $namespace = '';
+
/**
* {@inheritdoc}
*/
public function fetch($id)
{
- return $this->contains($id) ? xcache_get($id) : false;
+ return $this->contains($this->getNamespacedId($id)) ? xcache_get($this->getNamespacedId($id)) : false;
}
/**
@@ -47,7 +52,7 @@
*/
public function contains($id)
{
- return xcache_isset($id);
+ return xcache_isset($this->getNamespacedId($id));
}
/**
@@ -55,7 +60,7 @@
*/
public function save($id, $data, $lifeTime = false)
{
- return xcache_set($id, $data, $lifeTime);
+ return xcache_set($this->getNamespacedId($id), $data, $lifeTime);
}
/**
@@ -63,6 +68,30 @@
*/
public function delete($id)
{
- return xcache_unset($id);
+ return xcache_unset($this->getNamespacedId($id));
}
-}
\ No newline at end of file
+
+ /**
+ * Set the namespace where cache data will be stored. Useful for multiple, concurrent environments.
+ * @param string $namespace
+ */
+ public function setNamespace($namespace){
+ $this->namespace = $namespace;
+ }
+
+ /**
+ * Fetch the cache namespace.
+ * @return string $namespace
+ */
+ public function getNamespace(){
+ return $this->namespace;
+ }
+
+ /**
+ * Fetch the id, prepending with namespace, if set
+ * @param string id cache id
+ * @return string namespace id
+ */
+ private function getNamespacedId($id){
+ return $this->namespace?$this->namespace.'_'.$id:$id;
+ }}
\ No newline at end of file
Index: Common/Cache/MemcacheCache.php
===================================================================
--- Common/Cache/MemcacheCache.php (revision 6522)
+++ Common/Cache/MemcacheCache.php (working copy)
@@ -40,6 +40,11 @@
* @var Memcache
*/
private $_memcache;
+
+ /**
+ * @var namespace
+ */
+ private $namespace = '';
/**
* Sets the memcache instance to use.
@@ -66,7 +71,7 @@
*/
public function fetch($id)
{
- return $this->_memcache->get($id);
+ return $this->_memcache->get($this->getNamespacedId($id));
}
/**
@@ -74,7 +79,7 @@
*/
public function contains($id)
{
- return (bool) $this->_memcache->get($id);
+ return (bool) $this->_memcache->get($this->getNamespacedId($id));
}
/**
@@ -82,7 +87,7 @@
*/
public function save($id, $data, $lifeTime = false)
{
- return $this->_memcache->set($id, $data, 0, $lifeTime);
+ return $this->_memcache->set($this->getNamespacedId($id), $data, 0, $lifeTime);
}
/**
@@ -90,6 +95,31 @@
*/
public function delete($id)
{
- return $this->_memcache->delete($id);
+ return $this->_memcache->delete($this->getNamespacedId($id));
}
+
+ /**
+ * Set the namespace where cache data will be stored. Useful for multiple, concurrent environments.
+ * @param string $namespace
+ */
+ public function setNamespace($namespace){
+ $this->namespace = $namespace;
+ }
+
+ /**
+ * Fetch the cache namespace.
+ * @return string $namespace
+ */
+ public function getNamespace(){
+ return $this->namespace;
+ }
+
+ /**
+ * Fetch the id, prepending with namespace, if set
+ * @param string id cache id
+ * @return string namespace id
+ */
+ private function getNamespacedId($id){
+ return $this->namespace?$this->namespace.'_'.$id:$id;
+ }
}
\ No newline at end of file
Index: Common/Cache/ApcCache.php
===================================================================
--- Common/Cache/ApcCache.php (revision 6522)
+++ Common/Cache/ApcCache.php (working copy)
@@ -34,12 +34,17 @@
*/
class ApcCache implements Cache
{
+ /**
+ * @var namespace
+ */
+ private $namespace = '';
+
/**
* {@inheritdoc}
*/
public function fetch($id)
{
- return apc_fetch($id);
+ return apc_fetch($this->getNamespacedId($id));
}
/**
@@ -47,7 +52,7 @@
*/
public function contains($id)
{
- return apc_fetch($id) === false ? false : true;
+ return apc_fetch($this->getNamespacedId($id)) === false ? false : true;
}
/**
@@ -55,7 +60,7 @@
*/
public function save($id, $data, $lifeTime = false)
{
- return (bool) apc_store($id, $data, $lifeTime);
+ return (bool) apc_store($this->getNamespacedId($id), $data, $lifeTime);
}
/**
@@ -63,6 +68,31 @@
*/
public function delete($id)
{
- return apc_delete($id);
+ return apc_delete($this->getNamespacedId($id));
}
+
+ /**
+ * Set the namespace where cache data will be stored. Useful for multiple, concurrent environments.
+ * @param string $namespace
+ */
+ public function setNamespace($namespace){
+ $this->namespace = $namespace;
+ }
+
+ /**
+ * Fetch the cache namespace.
+ * @return string $namespace
+ */
+ public function getNamespace(){
+ return $this->namespace;
+ }
+
+ /**
+ * Fetch the id, prepending with namespace, if set
+ * @param string id cache id
+ * @return string namespace id
+ */
+ private function getNamespacedId($id){
+ return $this->namespace?$this->namespace.'_'.$id:$id;
+ }
}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment