Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there good solution for cache tagging on PHP/Redis?

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.

like image 537
SmxCde Avatar asked Oct 26 '15 23:10

SmxCde


People also ask

Is Redis good for caching?

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.

How connect PHP to Redis?

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:: " .

How does PHP store data in cache?

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.


2 Answers

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:

  • predis/predis
  • illuminate/redis
  • illuminate/cache

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
like image 173
Andrew Shell Avatar answered Sep 21 '22 21:09

Andrew Shell


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:

  • make sure to have enough memory for cache
  • put sets in different Redis instance, which doesn't have limited memory
  • put sets in completely different place (like DB)
  • dont' use taging, use redis hashes http://redis.io/commands/hset to create namespaces (you can group your keys by one "tag").
like image 26
my-nick Avatar answered Sep 24 '22 21:09

my-nick