Skip to content

Commit

Permalink
update upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mklooss committed Nov 20, 2024
2 parents b67d1ec + c2e14be commit ffc1b45
Show file tree
Hide file tree
Showing 16 changed files with 706 additions and 640 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: PHPUnit

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest

services:
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379

steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run PHPUnit test suite
run: composer run-script test

- name: Run PHP CS Fixer
run: composer run-script php-cs-fixer -- --dry-run
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.basedir
vendor
composer.lock
/.php-cs-fixer.cache
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* This document has been generated with
* https://mlocati.github.io/php-cs-fixer-configurator/#version:3.4.0|configurator
* you can change this configuration by importing this file.
*/
$config = new PhpCsFixer\Config();
return $config
->setRules([
'@PSR12' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->in([
'Cm/',
'tests/',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
);
610 changes: 344 additions & 266 deletions Cm/Cache/Backend/Redis.php

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM php:8.1-alpine
COPY --from=composer /usr/bin/composer /usr/bin/composer
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ modification, are permitted provided that the following conditions are met:
* The name of Colin Mollenhour may not be used to endorse or promote products
derived from this software without specific prior written permission.

* The class name must remain as Cm_Cache_Backend_Redis.
* If any files are modified, you must cause the modified files to carry prominent
notices stating that you changed the files and the date of any change.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand Down
80 changes: 46 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Zend_Cache backend using Redis with full support for tags

This Zend_Cache backend allows you to use a Redis server as a central cache storage. Tags are fully supported
without the use of TwoLevels cache so this backend is great for use on a single machine or in a cluster.
This `Zend_Cache` backend allows you to use a Redis server as a central cache storage. Tags are fully supported
without the use of `TwoLevels` cache so this backend is great for use on a single machine or in a cluster.
Works with any Zend Framework project including all versions of Magento!

## FEATURES
# FEATURES

- Uses the [phpredis PECL extension](https://github.com/nicolasff/phpredis) for best performance (requires **master** branch or tagged version newer than Aug 19 2011).
- Falls back to standalone PHP if phpredis isn't available using the [Credis](https://github.com/colinmollenhour/credis) library.
- Tagging is fully supported, implemented using the Redis "set" and "hash" datatypes for efficient tag management.
- Tagging is fully supported, implemented using the Redis "set" and "hash" data types for efficient tag management.
- Key expiration is handled automatically by Redis.
- Supports unix socket connection for even better performance on a single machine.
- Supports configurable compression for memory savings. Can choose between gzip, lzf and snappy and can change configuration without flushing cache.
- Uses transactions to prevent race conditions between saves, cleans or removes causing unexpected results.
- Supports a configurable "auto expiry lifetime" which, if set, will be used as the TTL when the key otherwise wouldn't expire. In combination with "auto expiry refresh on load" offers a more sane cache management strategy for Magento's `Enterprise_PageCache` module.
- __Unit tested!__

## REQUIREMENTS
# REQUIREMENTS

As this backend uses [Credis](https://github.com/colinmollenhour/credis) there are no additional requirements, but for improved performance you can install [phpredis](https://github.com/nicolasff/phpredis) which is a compiled extension.

Expand All @@ -25,21 +23,28 @@ As this backend uses [Credis](https://github.com/colinmollenhour/credis) there a
* phpredis does not support setting read timeouts at the moment (see pull request #260). If you receive read errors (“read error on connection”), this
might be the reason.

## INSTALLATION (Composer)
# INSTALLATION

```
$ composer require colinmollenhour/cache-backend-redis
Add the package as a dependency to your project with Composer.

```shell
composer require colinmollenhour/cache-backend-redis
```

## INSTALLATION (Magento)
### modman

You may use the Composer installation (above) or you can install via [modman](https://github.com/colinmollenhour/modman):
It is not the recommended method, but you may install via [modman](https://github.com/colinmollenhour/modman):

* `modman clone https://github.com/colinmollenhour/Cm_Cache_Backend_Redis`
```shell
modman clone https://github.com/colinmollenhour/Cm_Cache_Backend_Redis
```

# CONFIGURATION

### Magento Configuration:
These examples assume you are using Magento, but the configuration can just be passed to the constructor as a PHP
array with the same key names as seen in the examples.

Edit app/etc/local.xml to configure:
Edit `app/etc/local.xml` to configure:

<!-- This is a child node of config/global -->
<cache>
Expand Down Expand Up @@ -83,8 +88,6 @@ Edit app/etc/local.xml to configure:

## High Availability and Load Balancing Support

There are two supported methods of achieving High Availability and Load Balancing with Cm_Cache_Backend_Redis.

### Redis Sentinel

You may achieve high availability and load balancing using [Redis Sentinel](http://redis.io/topics/sentinel). To enable use of Redis Sentinel the `server`
Expand Down Expand Up @@ -146,7 +149,7 @@ with multiple addresses separated by a comma.
</backend_options>
</cache>

## ElastiCache
### ElastiCache

The following example configuration lets you use ElastiCache Redis (cluster mode disabled) where the writes are sent to
the Primary node and reads are sent to the replicas. This lets you distribute the read traffic between the different nodes.
Expand Down Expand Up @@ -177,11 +180,11 @@ Previously the ElastiCache config instructions suggested setting up a `<cluster>
and is no longer supported. The config is still parsed and loaded for backwards-compatibility but chooses a random slave
to read from rather than using md5 hash of the keys.

## RELATED / TUNING
# TUNING

- The recommended "maxmemory-policy" is "volatile-lru". All tag metadata is non-volatile so it is
- The recommended "maxmemory-policy" is "volatile-lru". All tag metadata is non-volatile, so it is
recommended to use key expirations unless non-volatile keys are absolutely necessary so that tag
data cannot get evicted. So, be sure that the "maxmemory" is high enough to accommodate all of
data cannot get evicted. So, be sure that the "maxmemory" is high enough to accommodate all
the tag data and non-volatile data with enough room left for the volatile key data as well.
- Automatic cleaning is optional and not recommended since it is slow and uses lots of memory.
- Occasional (e.g. once a day) garbage collection is recommended if the entire cache is infrequently cleared and
Expand All @@ -197,7 +200,9 @@ to read from rather than using md5 hash of the keys.
- Monitor your redis cache statistics with my modified [munin plugin](https://gist.github.com/1177716).
- Enable persistent connections. Make sure that if you have multiple configurations connecting the persistent
string is unique for each configuration so that "select" commands don't cause conflicts.
- Use the `stats.php` script to inspect your cache to find oversized or wasteful cache tags.
- Increase your server's `lua-time-limit` if you are getting "BUSY" errors. This setting can also cause Redis Sentinel
to invoke fail-overs when you would probably prefer to let the Lua script finish and have clients wait a little longer.
- Use the `stats.php` script to inspect your cache to find over-sized or wasteful cache tags.

### Example Garbage Collection Script (Magento)

Expand All @@ -210,20 +215,27 @@ to read from rather than using md5 hash of the keys.
// uncomment this for Magento Enterprise Edition
// Enterprise_PageCache_Model_Cache::getCacheInstance()->getFrontend()->getBackend()->clean('old');

## Release Notes

- March 2017: Added support for Redis Sentinel and loading from slaves. Thanks @Xon for the help!
- Sometime in 2013: Ceased updating these release notes...
- November 19, 2012: Added read_timeout option. (Feature only supported in standalone mode, will be supported by phpredis when pull request #260 is merged)
- October 29, 2012: Added support for persistent connections. (Thanks samm-git!)
- October 12, 2012: Improved memory usage and efficiency of garbage collection and updated recommendation.
- September 17, 2012: Added connect_retries option (default: 1) to prevent errors from random connection failures.
- July 10, 2012: Added password authentication support.
- Mar 1, 2012: Using latest Credis_Client which adds auto-reconnect for standalone mode.
- Feb 15, 2012: Changed from using separate keys for data, tags and mtime to a single hash per key.
- Nov 10, 2011: Changed from using phpredis and redisent to Credis (which wraps phpredis). Implemented pipelining.
# DEVELOPMENT

Please feel free to send Pull Requests to give back your improvements to the community!

You can run the unit tests locally with just Docker installed using a simple alias:

```shell
alias cm-cache-backend-redis='docker run --rm -it -e REDIS_SERVER=host.docker.internal -u $(id -u):$(id -g) -v ${COMPOSER_HOME:-$HOME/.composer}:/tmp -v $(pwd):/app --workdir /app cm-cache-backend-redis'
docker build . -t cm-cache-backend-redis
```

Then start a Redis server, install Composer dependencies and run tests like so:
```shell
docker run --rm -d -p 6379 --name cm-cache-backend-redis redis --maxmemory 20m --maxmemory-policy noeviction
cm-cache-backend-redis composer install
cm-cache-backend-redis composer run-script test
cm-cache-backend-redis composer run-script php-cs-fixer -- --dry-run
```

```
@copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com)
@copyright Copyright (c) 2022 Colin Mollenhour
This project is licensed under the "New BSD" license (see source).
```
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"colinmollenhour/cache-backend-redis",
"type":"magento-module",
"license":"BSD-3-Clause",
"license":"BSD-3-Clause-Modification",
"homepage":"https://github.com/colinmollenhour/Cm_Cache_Backend_Redis",
"description":"Zend_Cache backend using Redis with full support for tags.",
"authors":[
Expand All @@ -10,11 +10,21 @@
}
],
"require":{
"colinmollenhour/credis": "*"
"colinmollenhour/credis": "^1.14"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4",
"phpunit/phpunit": "^9",
"zf1s/zend-cache": "~1.15"
},
"autoload": {
"classmap": [
"Cm/Cache/Backend/Redis.php"
]
},
"scripts": {
"test": "vendor/bin/phpunit tests",
"test-performance": "PERFORMANCE=1 vendor/bin/phpunit tests --filter testLargePayloadLoop",
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --diff"
}
}
2 changes: 1 addition & 1 deletion modman
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Cm/Cache/Backend/* app/code/community/Cm/Cache/Backend/
Cm/Cache/Backend/Redis.php app/code/community/Cm/Cache/Backend/Redis.php
Loading

0 comments on commit ffc1b45

Please sign in to comment.