I am looking for easy way to store cache in Redis
and mark pieces of cache with tags
, so when I needed I could easily delete all the cache marked with specific tag.
Is there a good ready to use solution for that? (I am going to use access Redis with PHP)
I would do it by myself, as I understand I need to store tags as sets, where values are keys of cache, that use the tag. I even can cover the situation when I delete cache and its key should be removed from tag's set (I can store list of tags in cached element for that). But I am not sure how to do it when cache expires, in this case its key will "stuck" in a tag and next time when I delete cache by tag - it will clean cache with key, where that key may not be used anymore.
So I am looking for ready solution, at least to see how it is done.
Caching. Redis is a great choice for implementing a highly available in-memory cache to decrease data access latency, increase throughput, and ease the load off your relational or NoSQL database and application.
php //Connecting to Redis server on localhost $redis = new Redis(); $redis->connect('127.0. 0.1', 6379); echo "Connection to server sucessfully"; //set the data in redis string $redis->set("tutorial-name", "Redis tutorial"); // Get the stored data and print it echo "Stored string in redis:: " .
The easiest is to serialize() the data and store it in your database. When you need to retrieve the database, query it from the database, unserialize() it, and use it as before. As second approach is to add memcache to your PHP installation and access your data via the memcache functions.
You can do this with Illuminate\Cache which is a part of Laravel although can be used on it's own.
In order to configure it you need to have the following composer libraries installed:
Here is an example:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$servers = [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
];
$redis = new Illuminate\Redis\Database($servers);
$cache = new Illuminate\Cache\RedisStore($redis);
$cache->tags('posts', 'author_1')->put('post_1', 'Post 1 by Author 1', 1);
$cache->tags('posts', 'author_2')->put('post_2', 'Post 2 by Author 2', 1);
var_dump($cache->tags('posts', 'author_1')->get('post_1'));
var_dump($cache->tags('posts', 'author_2')->get('post_2'));
$cache->tags('author_2')->flush();
var_dump($cache->tags('posts', 'author_1')->get('post_1'));
var_dump($cache->tags('posts', 'author_2')->get('post_2'));
The result will be:
php test.php
string(18) "Post 1 by Author 1"
string(18) "Post 2 by Author 2"
string(18) "Post 1 by Author 1"
NULL
I would do it by myself, as I understand I need to store tags as sets, where values are keys of cache, that use the tag.
This will work if you will not limit memory avaiable for Redis (and usually there is hard limit for memory avaiable for cache). Because if Redis will remove set containing keys, and leave those keys you will not be able to delete them by tag.
Solutions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With