Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #182 from andrerom/micro_optimizations

Reduce function calls in creation of Item classes
  • Loading branch information...
commit 3c05be0bf082f2e5c959a125720cf7c21f216a4c 2 parents 6e455b6 + c6c11b6
@tedivm tedivm authored
View
2  src/Stash/Interfaces/ItemInterface.php
@@ -30,7 +30,7 @@ public function setPool(PoolInterface $driver);
* @param array $key
* @param string|null $namespace
*/
- public function setKey($key, $namespace = null);
+ public function setKey(array $key, $namespace = null);
/**
* This disables any IO operations by this object, effectively preventing
View
43 src/Stash/Item.php
@@ -151,16 +151,27 @@ class Item implements ItemInterface
public function setPool(PoolInterface $pool)
{
$this->pool = $pool;
- $this->setDriver($pool->getDriver());
+ $this->driver = $pool->getDriver();
}
/**
* {@inheritdoc}
*/
- public function setKey($key, $namespace = null)
+ public function setKey(array $key, $namespace = null)
{
$this->namespace = $namespace;
- $this->setupKey($key);
+
+ $keyStringTmp = $key;
+ if (isset($this->namespace)) {
+ array_shift($keyStringTmp);
+ }
+
+ $this->keyString = implode('/', $keyStringTmp);
+
+ // We implant the namespace "cache" to the front of every stash object's key. This allows us to segment
+ // off the user data, and use other 'namespaces' for internal purposes.
+ array_unshift($key, 'cache');
+ $this->key = array_map('strtolower', $key);
}
/**
@@ -590,30 +601,4 @@ public function __destruct()
$this->driver->clear($spkey);
}
}
-
- /**
- * This function is used by the Pool object while creating this object. It
- * is an internal function an should not be called directly.
- *
- * @param array $key
- * @throws \InvalidArgumentException
- */
- protected function setupKey($key)
- {
- if (!is_array($key)) {
- throw new \InvalidArgumentException('Item requires keys as arrays.');
- }
-
- $keyStringTmp = $key;
- if (isset($this->namespace)) {
- array_shift($keyStringTmp);
- }
-
- $this->keyString = implode('/', $keyStringTmp);
-
- // We implant the namespace "cache" to the front of every stash object's key. This allows us to segment
- // off the user data, and use other 'namespaces' for internal purposes.
- array_unshift($key, 'cache');
- $this->key = array_map('strtolower', $key);
- }
}
View
15 src/Stash/Pool.php
@@ -103,33 +103,30 @@ public function setItemClass($class)
public function getItem()
{
$args = func_get_args();
- $argCount = count($args);
- if ($argCount < 1) {
+ if (!isset($args[0])) {
throw new \InvalidArgumentException('Item constructor requires a key.');
}
// check to see if a single array was used instead of multiple arguments
- if ($argCount == 1 && is_array($args[0])) {
+ if (!isset($args[1]) && is_array($args[0])) {
$args = $args[0];
- $argCount = count($args);
}
- if ($argCount == 1) {
+ // if only one item treat as key string
+ if (!isset($args[1])) {
$keyString = trim($args[0], '/');
$key = explode('/', $keyString);
} else {
$key = $args;
}
- if (!($namespace = $this->getNamespace())) {
- $namespace = 'stash_default';
- }
+ $namespace = empty($this->namespace) ? 'stash_default' : $this->namespace;
array_unshift($key, $namespace);
foreach ($key as $node) {
- if (strlen($node) < 1) {
+ if (!isset($node[1]) && strlen($node) < 1) {
throw new \InvalidArgumentException('Invalid or Empty Node passed to getItem constructor.');
}
}
View
4 tests/Stash/Test/AbstractItemTest.php
@@ -152,8 +152,8 @@ public function testGet()
}
/**
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage Item requires keys as arrays.
+ * @expectedException PHPUnit_Framework_Error
+ * @expectedExceptionMessage Argument 1 passed to Stash\Item::setKey()
*/
public function testGetItemInvalidKey()
{
Please sign in to comment.
Something went wrong with that request. Please try again.