Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit fd1ea7a

Browse files
committed
Added keyspace prefix option
1 parent 5ec262c commit fd1ea7a

File tree

5 files changed

+873
-808
lines changed

5 files changed

+873
-808
lines changed

config/install/phpfastcache.settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
phpfastcache_enabled: false
22
phpfastcache_env: prod
3+
phpfastcache_prefix: ''
34
phpfastcache_default_ttl: 900
45
phpfastcache_htaccess: true
56
phpfastcache_default_driver: files

src/Cache/PhpFastCacheBackend.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ class PhpFastCacheBackend implements CacheBackendInterface
3333
*/
3434
protected $binPrefix;
3535

36+
/**
37+
* @var array
38+
*/
39+
protected $settings;
40+
3641
/**
3742
* Constructs a new PhpFastCacheBackend instance.
3843
*
3944
* @param $bin string
4045
* The name of the cache bin.
4146
* @param ExtendedCacheItemPoolInterface $cachePool
47+
* @param array $settings
4248
*/
43-
public function __construct($bin, $cachePool)
49+
public function __construct($bin, $cachePool, $settings)
4450
{
4551
$this->cachePool = $cachePool;
4652
$this->bin = $bin;
4753
$this->binPrefix = 'pfc::' . $this->bin . '::';
54+
$this->settings = $settings;
4855
}
4956

5057

@@ -102,7 +109,6 @@ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = [])
102109
$cacheObject = $this->getDrupalCacheStdObject();
103110
$cacheObject->cid = $cid;
104111
$cacheObject->data = $data;
105-
$cacheObject->expire = time();
106112
$cacheObject->expire = $expire;
107113
$cacheObject->tags = $tags;
108114
$cacheObject->serialized = false;
@@ -244,14 +250,25 @@ protected function getDrupalCacheStdObject()
244250
* @see DatabaseBackend::normalizeCid()
245251
*/
246252
protected function normalizeCid($cid) {
247-
// Nothing to do if the ID is a US ASCII string of 255 characters or less.
253+
/**
254+
* Add PhpFastCache Prefix
255+
*/
256+
$cid = $this->settings['phpfastcache_prefix'] . '-' . $cid;
257+
258+
/**
259+
* Nothing to do if the ID is a US ASCII string of 255 characters or less.
260+
*/
248261
$cid_is_ascii = mb_check_encoding($cid, 'ASCII');
249262
if (strlen($cid) <= 255 && $cid_is_ascii) {
250263
return $cid;
251264
}
252-
// Return a string that uses as much as possible of the original cache ID
253-
// with the hash appended.
265+
266+
/**
267+
* Return a string that uses as much as possible of the original cache ID
268+
* with the hash appended.
269+
*/
254270
$hash = Crypt::hashBase64($cid);
271+
255272
if (!$cid_is_ascii) {
256273
return $hash;
257274
}

src/Cache/PhpFastCacheBackendFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(Connection $connection)
6666
require_once __DIR__ . '/../../phpfastcache-php/src/autoload.php';
6767
require_once __DIR__ . '/../../phpssdb-php/src/autoload.php';
6868

69-
$this->backendClass = 'Drupal\phpfastcache\Cache\PhpFastCacheBackend';
69+
$this->backendClass = PhpFastCacheBackend::class;
7070
$this->connection = $connection;
7171
$this->settings = $this->getSettingsFromDatabase();
7272
$this->cachePool = $this->getPhpFastCacheInstance();
@@ -245,10 +245,10 @@ protected function getSettingsFromDatabase()
245245
* The cache backend object for the specified cache bin.
246246
*/
247247
public function get($bin) {
248-
if(in_array($bin, $this->settings['phpfastcache_bins'])){
249-
return new $this->backendClass($bin, $this->cachePool);
248+
if(in_array($bin, $this->settings['phpfastcache_bins']) || in_array('default', $this->settings['phpfastcache_bins'])){
249+
return new $this->backendClass($bin, $this->cachePool, $this->settings);
250250
}else{
251-
return new PhpFastCacheVoidBackend($bin, $this->cachePool);
251+
return new PhpFastCacheVoidBackend($bin, $this->cachePool, $this->settings);
252252
}
253253
}
254254
}

src/Cache/PhpFastCacheVoidBackend.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace Drupal\phpfastcache\Cache;
66

77
use Drupal\Core\Cache\Cache;
8-
use Drupal\Core\Cache\CacheTagsChecksumInterface;
9-
8+
use Drupal\Core\Cache\CacheBackendInterface;
9+
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
1010

1111
/**
1212
* Class PhpFastCacheService
1313
*/
14-
class PhpFastCacheVoidBackend implements \Drupal\Core\Cache\CacheBackendInterface
14+
class PhpFastCacheVoidBackend implements CacheBackendInterface
1515
{
1616
/**
1717
* The name of the cache bin to use.
@@ -29,18 +29,16 @@ class PhpFastCacheVoidBackend implements \Drupal\Core\Cache\CacheBackendInterfac
2929
*/
3030
protected $binPrefix;
3131

32-
public function __construct($bin, $cachePool)
32+
/**
33+
* Constructs a new PhpFastCacheBackend instance.
34+
*
35+
* @param $bin string
36+
* The name of the cache bin.
37+
* @param ExtendedCacheItemPoolInterface $cachePool
38+
* @param array $settings
39+
*/
40+
public function __construct($bin, $cachePool, $settings)
3341
{
34-
/**
35-
* Constructs a new ApcuBackend instance.
36-
*
37-
* @param string $bin
38-
* The name of the cache bin.
39-
* @param string $site_prefix
40-
* The prefix to use for all keys in the storage that belong to this site.
41-
* @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
42-
* The cache tags checksum provider.
43-
*/
4442
}
4543

4644

0 commit comments

Comments
 (0)