alex19pov31/bitrix-redis-cache

Bitrix Redis 缓存引擎

v0.1.2 2019-03-21 09:27 UTC

This package is auto-updated.

Last update: 2024-09-21 22:39:04 UTC


README

Latest Stable Version

Bitrix Redis 缓存

通过 Redis 缓存数据。

安装

composer require alex19pov31/bitrix-redis-cache

配置

在文件 /bitrix/.settings_extra.php(如果不存在,则创建)中填写配置

<?php
return [
  'cache' => [
    'value' => [
      'type' => [
          'class_name' => 'Alex19pov31\BitrixRedisCache\RedisCacheEngine',
        ],
      'redis' => [
        'scheme' => 'tcp',
        'host' => '127.0.0.1',
        'port' => 6379,
      ],
      'sid' => $_SERVER["DOCUMENT_ROOT"]."#01"
    ],
  ],
];

关于连接参数的详细说明请参阅此处 - https://github.com/nrk/predis/wiki/Connection-Parameters

需要在 /bitrix/dbconn.php 中连接自动加载器。

使用

use Bitrix\Main\Data\Cache;

$data = null;
$ttl = 3600; // кешируем на час
$key = "test_key"; // ключ кеша

$cache = Cache::createInstance();
if ($cache->initCache($ttl, $key, 'redis')) {
    $data = $cache->getVars();
    // или если имело место быть кеширование вывода
    $cache->output();
} elseif ($cache->startDataCache($ttl, $key, 'redis', [])) {
	$data = 'Тестовые данные';
	$cache->endDataCache($data);
}

强制使用(无配置文件)

use Alex19pov31\BitrixRedisCache\RedisCacheEngine;
use Bitrix\Main\Data\Cache;

$cacheEngine = new RedisCacheEngine([
	'scheme' => 'tcp',
	'host' => '127.0.0.1',
	'port' => 6379,
]);

$data = null;
$ttl = 3600; // кешируем на час
$key = "test_key"; // ключ кеша

$cache = new Cache($cacheEngine);
if ($cache->initCache($ttl, $key, 'redis')) {
    $data = $cache->getVars();
    // или если имело место быть кеширование вывода
    $cache->output();
} elseif ($cache->startDataCache($ttl, $key, 'redis', [])) {
	$data = 'Тестовые данные';
	$cache->endDataCache($data);
}