Skip to content

Commit d5157c1

Browse files
committed
initial commit
0 parents  commit d5157c1

5 files changed

+15659
-0
lines changed

class-phpredis-purger.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* Handles purging Redis via the built-in PHP Redis class
5+
*/
6+
class Php_Redis_Purger {
7+
8+
/**
9+
* PHP Redis API object
10+
*
11+
* @var object $redis_object PHP Redis API object
12+
*/
13+
public $redis_object;
14+
15+
/**
16+
* Get an instance of this class
17+
*/
18+
public static function get_instance() {
19+
static $instance = null;
20+
if ( null === $instance ) {
21+
$instance = new static();
22+
$instance->setup();
23+
$instance->setup_actions();
24+
}
25+
return $instance;
26+
}
27+
28+
/**
29+
* Setup connection to Redis
30+
*/
31+
public function setup() {
32+
33+
if ( ! class_exists( 'Redis' ) ) {
34+
return;
35+
}
36+
37+
try {
38+
$connection_details = Redis_Full_Page_Cache_Purger::get_connection_details();
39+
$this->redis_object = new Redis();
40+
$this->redis_object->connect(
41+
$connection_details->host,
42+
$connection_details->port,
43+
5
44+
);
45+
46+
} catch ( Exception $e ) {
47+
erorr_log( $e->getMessage() );
48+
}
49+
}
50+
51+
/**
52+
* Hook into the Redis purger via actions
53+
*/
54+
public function setup_actions() {
55+
add_action( 'redis_cache_purge/purge_single_key', array( $this, 'delete_single_key' ) );
56+
add_action( 'redis_cache_purge/purge_wildcard_key', array( $this, 'delete_keys_by_wildcard' ) );
57+
}
58+
59+
/**
60+
* Delete a single key from Redis
61+
* e.g. $key can be nginx-cache:httpGETexample.com/
62+
*
63+
* @param string $key Cache key to be deleted
64+
*/
65+
public function delete_single_key( $key ) {
66+
try {
67+
$this->redis_object->del( $key );
68+
} catch ( Exception $e ) {
69+
erorr_log( $e->getMessage() );
70+
}
71+
}
72+
73+
/**
74+
* Delete one or more keys based on wildcard pattern
75+
* e.g. $key can be nginx-cache:httpGETexample.com*
76+
*
77+
* Lua Script block to delete multiple keys using wildcard
78+
* script will return number of keys deleted
79+
* if return value is 0, that means no matches were found
80+
*
81+
* Call redis eval and return value from lua script
82+
*
83+
* @param string $pattern Cache key pattern
84+
*/
85+
public function delete_keys_by_wildcard( $pattern ) {
86+
87+
// Lua Script.
88+
$lua = <<<LUA
89+
local k = 0
90+
for i, name in ipairs(redis.call('KEYS', KEYS[1]))
91+
do
92+
redis.call('DEL', name)
93+
k = k+1
94+
end
95+
return k
96+
LUA;
97+
98+
try {
99+
$this->redis_object->eval( $lua, array( $pattern ), 1 );
100+
} catch ( Exception $e ) {
101+
error_log( $e->getMessage() );
102+
}
103+
}
104+
105+
}
106+
Php_Redis_Purger::get_instance();

class-predis-purger.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
* Handles purging Redis via the 3rd party Predis class
5+
*/
6+
class Predis_Purger {
7+
8+
/**
9+
* Predis API object
10+
*
11+
* @var object $redis_object Predis API object
12+
*/
13+
public $redis_object;
14+
15+
/**
16+
* Get an instance of this class
17+
*/
18+
public static function get_instance() {
19+
static $instance = null;
20+
if ( null === $instance ) {
21+
$instance = new static();
22+
$instance->setup();
23+
$instance->setup_actions();
24+
}
25+
return $instance;
26+
}
27+
28+
/**
29+
* Setup connection to Redis
30+
*/
31+
public function setup() {
32+
33+
if ( ! class_exists( 'Predis\Autoloader' ) ) {
34+
require_once REDIS_CACHE_PURGE_PLUGIN_DIR_PATH . 'predis.php';
35+
}
36+
37+
Predis\Autoloader::register();
38+
39+
$connection_details = Redis_Full_Page_Cache_Purger::get_connection_details();
40+
$this->redis_object = new Predis\Client(
41+
array(
42+
'host' => $connection_details->host,
43+
'port' => $connection_details->port,
44+
)
45+
);
46+
47+
try {
48+
$this->redis_object->connect();
49+
} catch ( Exception $e ) {
50+
error_log( $e->getMessage() );
51+
}
52+
53+
}
54+
55+
/**
56+
* Hook into the Redis purger via actions
57+
*/
58+
public function setup_actions() {
59+
add_action( 'redis_cache_purge/purge_single_key', array( $this, 'delete_single_key' ) );
60+
add_action( 'redis_cache_purge/purge_wildcard_key', array( $this, 'delete_keys_by_wildcard' ) );
61+
}
62+
63+
/**
64+
* Delete a single key from Redis
65+
* e.g. $key can be nginx-cache:httpGETexample.com/
66+
*
67+
* @param string $key Cache key to be deleted
68+
*/
69+
public function delete_single_key( $key ) {
70+
try {
71+
$this->redis_object->executeRaw( array( 'DEL', $key ) );
72+
} catch ( Exception $e ) {
73+
error_log( $e->getMessage() );
74+
}
75+
}
76+
77+
/**
78+
* Delete one or more keys based on wildcard pattern
79+
* e.g. $key can be nginx-cache:httpGETexample.com*
80+
*
81+
* Lua Script block to delete multiple keys using wildcard
82+
* script will return number of keys deleted
83+
* if return value is 0, that means no matches were found
84+
*
85+
* Call redis eval and return value from lua script
86+
*
87+
* @param string $pattern Cache key pattern
88+
*/
89+
public function delete_keys_by_wildcard( $pattern ) {
90+
91+
// Lua Script.
92+
$lua = <<<LUA
93+
local k = 0
94+
for i, name in ipairs(redis.call('KEYS', KEYS[1]))
95+
do
96+
redis.call('DEL', name)
97+
k = k+1
98+
end
99+
return k
100+
LUA;
101+
102+
try {
103+
return $this->redis_object->eval( $lua, 1, $pattern );
104+
} catch ( Exception $e ) {
105+
error_log( $e->getMessage() );
106+
}
107+
}
108+
109+
}

0 commit comments

Comments
 (0)