Skip to content

Commit e7cef67

Browse files
committed
prefix in cache
1 parent 4c19d17 commit e7cef67

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Cache/Adapter/FileCache.php

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct($server = null, $ttl = 0, array $config = [])
1818

1919
public function set($key, $value, $ttl = -1)
2020
{
21+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
2122
$filename = "{$this->server}/" . md5($key);
2223
$ttl = $ttl <= 0 ? $this->ttl : $ttl;
2324
$expired = $ttl ? time() + $ttl : PHP_INT_MAX;
@@ -31,6 +32,7 @@ public function set($key, $value, $ttl = -1)
3132

3233
public function get($key)
3334
{
35+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
3436
$filename = "{$this->server}/" . md5($key);
3537
$data = file_get_contents($filename);
3638
list($value, $expired) = unserialize($data);
@@ -48,12 +50,14 @@ public function get($key)
4850

4951
public function delete($key)
5052
{
53+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
5154
$filename = "{$this->server}/" . md5($key);
5255
unlink($filename);
5356
}
5457

5558
public function exist($key)
5659
{
60+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
5761
$filename = "{$this->server}/" . md5($key);
5862
return file_exists($filename);
5963
}

Cache/Adapter/MemCache.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public function __construct($server = null, $ttl = 0, array $config = [])
2424

2525
public function set($key, $value, $ttl = -1)
2626
{
27+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
2728
$this->memcache->set($key, $value, null, $ttl <= 0 ? $this->ttl : $ttl);
2829
}
2930

3031
public function get($key)
3132
{
33+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
3234
$flag = false;
3335
$result = $this->memcache->get($key, $flag);
3436
if (!is_bool($flag) || !$flag)
@@ -38,6 +40,7 @@ public function get($key)
3840

3941
public function check($key, $setter, $ttl = -1)
4042
{
43+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
4144
$flag = false;
4245
$result = $this->memcache->get($key, $flag);
4346
if ($flag)
@@ -50,6 +53,7 @@ public function check($key, $setter, $ttl = -1)
5053

5154
public function delete($key)
5255
{
56+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
5357
return $this->memcache->delete($key);
5458
}
5559

Cache/Adapter/SimpleCache.php

+4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public function __construct($server = null, $ttl = 0, array $config = [])
1515

1616
public function set($key, $value, $ttl = -1)
1717
{
18+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
1819
$ttl = $ttl <= 0 ? $this->ttl : $ttl;
1920
$expired = $ttl ? time() + $ttl : PHP_INT_MAX;
2021
$this->cache[$key] = [$value, $expired];
2122
}
2223

2324
public function get($key)
2425
{
26+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
2527
list($value, $expired) = $this->cache[$key];
2628

2729
if(time() > $expired)
@@ -37,11 +39,13 @@ public function get($key)
3739

3840
public function delete($key)
3941
{
42+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
4043
unset($this->cache[$key]);
4144
}
4245

4346
public function exist($key)
4447
{
48+
$key = isset($this->config["prefix"]) ? "{$this->config["prefix"]}{$key}" : $key;
4549
/** @noinspection PhpUnusedLocalVariableInspection */
4650
list($value, $expired) = $this->cache[$key];
4751

0 commit comments

Comments
 (0)