Skip to content

Commit e64d0a2

Browse files
Tobias BohmanTobias Bohman
authored andcommitted
Merge pull request #1 from Nyholm/long_keys
Fix for long keys
2 parents ac6860c + 1147496 commit e64d0a2

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"ext-memcached": "*",
3232
"psr/cache": "1.0.0",
3333
"cache/adapter-common": "^0.1",
34-
"cache/taggable-cache": "^0.4",
34+
"cache/taggable-cache": "^0.3",
3535
"cache/hierarchical-cache": "^0.1"
3636
},
3737
"require-dev":

src/MemcachedCachePool.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(\Memcached $cache)
4040

4141
protected function fetchObjectFromCache($key)
4242
{
43-
return $this->cache->get($this->getHierarchyKey($key));
43+
return $this->cache->get($this->trimKey($this->getHierarchyKey($key)));
4444
}
4545

4646
protected function clearAllObjectsFromCache()
@@ -51,8 +51,8 @@ protected function clearAllObjectsFromCache()
5151
protected function clearOneObjectFromCache($key)
5252
{
5353
$this->commit();
54-
$key = $this->getHierarchyKey($key, $path);
55-
$this->cache->increment($path, 1, 0);
54+
$key = $this->trimKey($this->getHierarchyKey($key, $path));
55+
$this->cache->increment($this->trimKey($path), 1, 0);
5656
$this->clearHierarchyKeyCache();
5757

5858
if ($this->cache->delete($key)) {
@@ -69,13 +69,28 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
6969
$ttl = 0;
7070
}
7171

72-
$key = $this->getHierarchyKey($key);
72+
$key = $this->trimKey($this->getHierarchyKey($key));
7373

7474
return $this->cache->set($key, $item, $ttl);
7575
}
7676

7777
protected function getValueFormStore($key)
7878
{
79-
return $this->cache->get($key);
79+
return $this->cache->get($this->trimKey($key));
80+
}
81+
82+
/**
83+
* Calculate a key. If it is more than 250 chars we should hash the key.
84+
*
85+
* @param $key
86+
* @param null $ref
87+
*/
88+
private function trimKey($key)
89+
{
90+
if (strlen($key) < 250) {
91+
return $key;
92+
}
93+
// This should maybe be logged
94+
return sha1($key);
8095
}
8196
}

0 commit comments

Comments
 (0)