Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis Cache #152

Open
fhferreira opened this issue Aug 3, 2021 · 2 comments
Open

Redis Cache #152

fhferreira opened this issue Aug 3, 2021 · 2 comments

Comments

@fhferreira
Copy link

Is possible to add a cache with redis?
Something like:

<?php

/*
 * This file is part of the Liquid package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package Liquid
 */

namespace App\Helpers\Liquid\Cache;

use Liquid\Cache;
use Liquid\LiquidException;
use Illuminate\Support\Facades\Redis as RedisReader;

/**
 * Implements cache stored in Redis.
 *
 * @codeCoverageIgnore
 */
class Redis extends Cache
{
	/**
	 * Constructor.
	 *
	 * It checks the availability of redis.
	 *
	 * @param array $options
	 *
	 * @throws LiquidException if redis extension is not loaded or is disabled.
	 */
	public function __construct(array $options = array())
	{
		parent::__construct($options);

		if (!extension_loaded('redis')) {
			throw new LiquidException(get_class($this).' requires PHP redis extension or similar to be loaded.');
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function read($key, $unserialize = true)
	{
        $redis = RedisReader::connection();
        return $redis->get($this->prefix . $key);
	}

	/**
	 * {@inheritdoc}
	 */
	public function exists($key)
	{
        $redis = RedisReader::connection();
        return $redis->exists($this->prefix . $key);
	}

	/**
	 * {@inheritdoc}
	 */
	public function write($key, $value, $serialize = true)
	{
        $redis = RedisReader::connection();
        //set the key
        $redis->set($this->prefix . $key, $value);
        //set the expiration
        //I understand this means expire in 60s.
        $redis->expire($this->prefix . $key,$this->expire);

        return $value;
	}

	/**
	 * {@inheritdoc}
	 */
	public function flush($expiredOnly = false)
	{
        return RedisReader::command('flushdb');
    }
}

@sanmai
Copy link
Collaborator

sanmai commented Aug 4, 2021

You can add your own cache implementation IIRC, and, being completely clear, the cache isn't working for more anywhere more complex cases. Expect all kinds of bugs if you need to include templates with variables.

@fhferreira
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants