Narzędzia użytkownika

Narzędzia witryny


redis_-_konfiguracja

Cache

'components' => [
    // ...
    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
            'hostname' => 'localhost', // Adres hosta Redis
            'port' => 6379,             // Numer portu Redis
            'database' => 0,            // Numer bazy danych Redis
        ],
    ],
    // ...
],

Sesja w Redis

'components' => [
    'session' => [
        'class' => 'yii\redis\Session',
        'timeout' => 86400, // czas ważności w sekundach (np. 1 dzień)
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
    ],
],

Konfiguracja awaryjna cache w Redis

Sposób konfiguracji badajacy czy Redis jest aktualnie dostępny. Jeśli nie cache przekierowywane jest do plików

common/main.php

'components' => [
 
        'cache' => function () {
            $cache = new \yii\caching\FileCache();
            $connection = @fsockopen('localhost', 6379, $errorCode, $errorMsg, 1);
            if ($connection) {
                $cache = new  \yii\redis\Cache([
                    'redis' => [
                        'hostname' => 'localhost', // Adres hosta Redis
                        'port' => 6379, // Numer portu Redis
                        'database' => 0, // Numer bazy danych Redis
                    ],
                ]);
                fclose($connection);
            }
 
            return \yii\di\Instance::ensure($cache, 'yii\caching\CacheInterface');
        },
 
        ...

common/main-local.php

'redis' => function () {
            // Sprawdzenie dostępności serwera Redis
            $connection = @fsockopen('localhost', 6379, $errorCode, $errorMsg, 1);
            if (!$connection) {
                return false;
            }
            fclose($connection);
 
            // Zwróć skonfigurowany komponent Redis
            $red = new  \yii\redis\Cache([
                'redis' => [
                    'hostname' => 'localhost', // Adres hosta Redis
                    'port' => 6379, // Numer portu Redis
                    'database' => 0, // Numer bazy danych Redis
                ],
            ]);
            return $red;
        },

Testowanie cache

public function actionIndex() {
 
        // Pobranie komponentu Redis
        if ($redis = Yii::$app->redis) {
 
            // Zapis danych do Redis
            $redis->set('test_key', 'Hello tu Redis. Działam poprawnie :-)');
 
            // Odczyt danych z Redis
            $value = $redis->get('test_key');
            echo $value;
        } else {
 
            echo 'Redis nie został załadowany';
        }
        echo '<hr>';
        // Pobierz obiekt aplikacji Yii2
        $application = Yii::$app;
 
        // Pobierz komponent cache'u
        $cacheComponent = $application->getCache();
 
        // Sprawdź, czy komponent cache'u został skonfigurowany
        if ($cacheComponent !== null) {
            // Pobierz nazwę klasy komponentu cache'u
            $cacheClassName = get_class($cacheComponent);
 
            // Wyświetl informacje o bieżącym miejscu przechowywania cache'u
            echo "Cache aktualnie używa: $cacheClassName";
        } else {
            // Jeśli komponent cache'u nie został skonfigurowany, wyświetl odpowiedni komunikat
            echo "Cache component nie jest skonfigurowany.";
        }
 
        exit;
    }
redis_-_konfiguracja.txt · ostatnio zmienione: 2024/09/15 16:22 przez 127.0.0.1