Skip to content

Commit

Permalink
Merge pull request #10 from Traackr/CAMP-881-redis-sets
Browse files Browse the repository at this point in the history
CAMP-881: Add CacheEnginesHelper::writeWithParent.
  • Loading branch information
jec3 authored Dec 3, 2020
2 parents fee710b + d2a94bd commit 74534b3
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 43 deletions.
1 change: 1 addition & 0 deletions Config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
// Bootstrap for CakePHP plugin loading
// Loads required classes and all engines in the plugin
App::uses('FileEngine', 'Cache/Engine');
require_once(dirname(__FILE__) . '/../src/CacheEnginesHelper.php');
require_once(dirname(__FILE__) . '/../src/Engines.php');
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM php:7.1-fpm-alpine
RUN apk update && apk add build-base
RUN apk add zlib-dev git zip libmcrypt-dev \
&& docker-php-ext-install zip \
&& docker-php-ext-install mcrypt \
&& docker-php-ext-enable mcrypt
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
&& ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
COPY . /app
WORKDIR /app
ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}"
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
help: ## This command
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help

build: ## Builds the image using docker-compose
docker-compose build --no-cache cakephp-cache-engines
start: ## Run the application in the background
docker-compose up -d
start-build: ## Build the application and run application
docker-compose up -d --force-recreate --remove-orphans
stop: ## Stop application
@docker-compose stop
run-composer-install: ## Run composer install
docker-compose exec -T cakephp-cache-engines \
composer install
run-unit-tests: ## Run unit tests
docker-compose exec -T cakephp-cache-engines \
/app/bin/phpunit test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ This CakePHP plugin provides some additional cache engines that can be used by C

We currently provide three cache engines:

1. RedisTreeCacheEngine: Redis based cache that supports managing keys using wildcards
2. FileTreeCacheEngine: Local filesystem based cache that supports managing keys using wildcards
1. RedisTreeCacheEngine: Redis based cache that supports managing keys using wildcards and cache key 'parents'.
2. FileTreeCacheEngine: Local filesystem based cache that supports managing keys using wildcards and cache key 'parents'.
3. FallBackCacheEngine: Allows you to define two cache engines; the first engine is used as the primary cache engine.
The second cache engine is used only if the primary fails.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"require-dev": {
"phpunit/phpunit": "4.8.*",
"cakephp/cakephp": "2.8.*",
"cakephp/cakephp": "2.10.22",
"m6web/redis-mock": "v2.8.*"
},
"config": {
Expand Down
10 changes: 9 additions & 1 deletion doc/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ You are now ready to run the tests:

```bash
$ bin/phpunit test
```
```

The tests can also be ran using Docker with the following commands:

```bash
$ make start-build
$ make run-composer-install
$ make run-unit-tests
```
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.7"
services:
cakephp-cache-engines:
build:
context: .
dockerfile: Dockerfile
network: host
container_name: cakephp-cache-engines
restart: always
volumes:
- ./:/app:rw
121 changes: 121 additions & 0 deletions src/CacheEnginesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* Helper utility methods for the advanced features offered by
* some Cache Engines, such as the RedisTreeEngine.
*/
class CacheEnginesHelper
{
/**
* Write data for key into a cache engine with one or more 'parent'.
*
* The following is a modified version of:
* https://github.com/cakephp/cakephp/blob/2.10.22/lib/Cake/Cache/Cache.php
* The modifications are limited to the addition of
* the `$parentKey` parameter.
*
* ### Usage:
*
* Write the value for a single key with a single parent:
*
* `Cache::write('cache_key', $data, $config, 'parent_cache_key');`
*
* Write the value for a single key with multiple parents:
*
* `Cache::write('cache_key', $data, $config, [
* 'parent_cache_key_1',
* 'parent_cache_key_2',
* ]);`
*
* Write the values for multiple keys with the same parent:
*
* `Cache::write(
* '[cache_key_1,cache_key_2]',
* $data,
* $config,
* 'parent_cache_key_1'
* );`
*
* Write the values for multiple keys with the same parents:
*
* `Cache::write(
* '[cache_key_1,cache_key_2]',
* $data,
* $config,
* [
* 'parent_cache_key_1',
* 'parent_cache_key_2',
* ]
* );`
* Write the values for multiple keys with different parents:
*
* `Cache::write(
* '[cache_key_1,cache_key_2]',
* $data,
* $config,
* [
* 'cache_key_1' => [
* 'parent_cache_key_1'
* ],
* 'cache_key_2' => [
* 'parent_cache_key_2'
* ]
* ]
* );`
*
* Writing to a specific cache config:
*
* `Cache::write('cached_data', $data, 'long_term');`
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached - anything except a resource
* @param string $config Optional string configuration name to write to
* Defaults to 'default'
* @param string|array $parentKey Parent key that data is a dependent child of
* @return bool True if the data was successfully cached, false on failure
*/
public static function writeWithParent(
$key,
$value,
$config = 'default',
$parentKey = ''
) {
$settings = Cache::settings($config);

if (empty($settings)) {
return false;
}
if (!Cache::isInitialized($config)) {
return false;
}
$key = Cache::engine($config)->key($key);

if (!$key || is_resource($value)) {
return false;
}

$success = false;
if (method_exists(Cache::engine($config), 'writeWithParent')) {
$success = Cache::engine($config)->writeWithParent(
$settings['prefix'] . $key,
$value,
$settings['duration'],
$parentKey
);
}
Cache::set(null, $config);
if ($success === false && $value !== '') {
trigger_error(
__d(
'cake_dev',
"%s cache was unable to write '%s' to %s cache",
$config,
$key,
Cache::$_engines[$config]->settings['engine']
),
E_USER_WARNING
);
}
return $success;
}
}
20 changes: 20 additions & 0 deletions src/FallbackEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public function write($key, $value, $duration)
}
}

/**
* Write data for key into a cache engine with one or more 'parent'.
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached
* @param integer $duration How long to cache the data, in seconds
* @param string|array $parentKey Parent key that data is a dependent child of
* @return bool True if the data was successfully cached, false on failure
* @throws Exception
*/
public function writeWithParent($key, $value, $duration, $parentKey = '')
{
return Cache::engine($this->activeCache)->writeWithParent($key, $value, $duration, $parentKey);
}

public function read($key)
{
try {
Expand Down Expand Up @@ -130,4 +145,9 @@ protected function fallback($setPrimary = false)
$this->activeCache = $this->secondaryConfig;
}
}

public function key($key)
{
return Cache::engine($this->activeCache)->key($key);
}
}
21 changes: 20 additions & 1 deletion src/FileTreeEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public function write($key, $data, $duration)

}

/**
* 'Parents' are not supported by the FileTreeEngine.
* This method performs same action as `write`.
*
* This method exists to gracefully degrade when using
* this engine as a fallback to the RedisTreeEngine.
*
* @param string $key
* @param mixed $data
* @param int $duration
* @param string|array $parentKey Unused.
* @return bool
* @throws Exception
*/
public function writeWithParent($key, $data, $duration, $parentKey = '')
{
return $this->write($key, $data, $duration);
}


public function delete($key)
{
Expand Down Expand Up @@ -151,4 +170,4 @@ public function key($key)
return str_replace(array(DS, '/', '.', '<', '>', '?', ':', '|', ' ', '"'), '_', $key);

}
}
}
Loading

0 comments on commit 74534b3

Please sign in to comment.