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

Cache providers can key clash when multiple apps are using the same provider engine #334

Closed
alphadevx opened this issue Jun 2, 2018 · 1 comment
Assignees
Labels
Milestone

Comments

@alphadevx
Copy link
Owner

No description provided.

@alphadevx alphadevx added the bug label Jun 2, 2018
@alphadevx alphadevx added this to the 3.0.0 milestone Jun 2, 2018
@alphadevx alphadevx self-assigned this Jun 2, 2018
@alphadevx
Copy link
Owner Author

alphadevx commented Jun 2, 2018

Example fix is to provide a key prefix (need to unit test this):

class CacheProviderRedis implements CacheProviderInterface
{
    /**
     * Trace logger.
     *
     * @var \Alpha\Util\Logging\Logger
     *
     * @since 1.2.4
     */
    private static $logger = null;

    /**
     * Connection to the cache server.
     *
     * @var Redis
     *
     * @since 1.2.4
     */
    private $connection;

    /**
     * Cache key prefix to use, based on the application title, to prevent key clashes between different apps
     * using the same cache provider.
     *
     * @var string
     *
     * @since 3.0.0
     */
    private $appPrefix;

    /**
     * Constructor.
     *
     * @since 1.2.4
     */
    public function __construct()
    {
        self::$logger = new Logger('CacheProviderRedis');

        $config = ConfigProvider::getInstance();

        $this->appPrefix = base64_encode($config->get('app.title'));

        try {
            $this->connection = new Redis();
            $this->connection->connect($config->get('cache.redis.host'), $config->get('cache.redis.port'));
            $this->connection->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
            $this->connection->select($config->get('cache.redis.db'));
        } catch (\Exception $e) {
            self::$logger->error('Error while attempting to connect to Redis cache: ['.$e->getMessage().']');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get($key)
    {
        self::$logger->debug('>>get(key=['.$key.'])');

        try {
            $value = $this->connection->get($this->appPrefix.'-'.$key);

            self::$logger->debug('<<get: ['.print_r($value, true).'])');

            return $value;
        } catch (\Exception $e) {
            self::$logger->error('Error while attempting to load a business object from Redis instance: ['.$e->getMessage().']');
            self::$logger->debug('<<get: [false])');

            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $expiry = 0)
    {
        try {
            if ($expiry > 0) {
                $this->connection->setex($this->appPrefix.'-'.$key, $expiry, $value);
            } else {
                $this->connection->set($this->appPrefix.'-'.$key, $value);
            }
        } catch (\Exception $e) {
            self::$logger->error('Error while attempting to store a value to Redis instance: ['.$e->getMessage().']');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function delete($key)
    {
        try {
            $this->connection->delete($this->appPrefix.'-'.$key);
        } catch (\Exception $e) {
            self::$logger->error('Error while attempting to remove a value from Redis instance: ['.$e->getMessage().']');
        }
    }
}

alphadevx added a commit that referenced this issue Jun 10, 2018
…rface providers to ensure that multiple apps using the same caching service do not have a key clash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant