PHP code example of stoyishi / cache

1. Go to this page and download the library: Download stoyishi/cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stoyishi / cache example snippets




use Stoyishi\Cache\FileCache;

use Stoyishi\Cache\FileCache;


// Keshni ishga tushirish
$cache = new FileCache('/path/to/cache/directory');

// Kesh elementini saqlash
$cache->set('user_123', ['name' => 'John Doe', 'email' => '[email protected]']);

// Kesh elementini olish
$user = $cache->get('user_123');

// Element mavjudligini tekshirish
if ($cache->has('user_123')) {
    echo "Kesh topildi!";
}

// Kesh elementini o'chirish
$cache->delete('user_123');

// Barcha keshni tozalash
$cache->clear();

// 1 soat davomida keshlash (3600 soniya)
$cache->set('session_data', $sessionData, 3600);

// 1 kun davomida keshlash (standart TTL 86400 soniya)
$cache->set('daily_stats', $stats);

// Doimiy saqlash (yoki qo'lda o'chirilmaguncha)
$cache->set('permanent_data', $data, PHP_INT_MAX);

// Bir vaqtning o'zida bir nechta elementlarni saqlash
$cache->setMultiple([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
], 3600);

// Bir vaqtning o'zida bir nechta elementlarni olish
$values = $cache->getMultiple(['key1', 'key2', 'key3']);

// Bir nechta elementlarni o'chirish
$cache->deleteMultiple(['key1', 'key2', 'key3']);

// Agar kalit topilmasa, standart qiymatni qaytarish
$value = $cache->get('mavjud_emas', 'standart_qiymat');

// Natija: 'standart_qiymat'

public function __construct(string $path)

public function get($key, $default = null)

public function set($key, $value, $ttl = null)

public function delete($key)

public function clear()

public function has($key)

public function getMultiple($keys, $default = null)

public function setMultiple($values, $ttl = null)

public function deleteMultiple($keys)

// 1. Ma'noli kesh kalitlaridan foydalaning
$cache->set('user_profile_' . $userId, $profile);

// 2. Ma'lumotlar o'zgaruvchanligi asosida mos TTL o'rnating
$cache->set('real_time_stock', $price, 60);      // 1 daqiqa
$cache->set('daily_report', $report, 86400);     // 1 kun
$cache->set('static_config', $config, 604800);   // 1 hafta

// 3. Kesh topilmasa, to'g'ri ishlov bering
$data = $cache->get('expensive_query');
if ($data === null) {
    $data = performExpensiveQuery();
    $cache->set('expensive_query', $data, 3600);
}

// 4. Imkon qadar ommaviy operatsiyalardan foydalaning
$cache->setMultiple($bulkData);



use Stoyishi\Cache\FileCache;

// Keshni yaratish
$cache = new FileCache(__DIR__ . '/cache');

// Foydalanuvchi ma'lumotlarini keshlash
$userId = 123;
$userKey = 'user_' . $userId;

// Ma'lumotni keshda tekshirish
$user = $cache->get($userKey);

if ($user === null) {
    // Keshda yo'q, ma'lumotlar bazasidan olish
    $user = [
        'id' => $userId,
        'name' => 'Aziz Rahimov',
        'email' => '[email protected]',
        'phone' => '+998901234567'
    ];
    
    // 1 soat davomida keshlash
    $cache->set($userKey, $user, 3600);
    echo "Ma'lumot bazasidan olindi va keshlandi\n";
} else {
    echo "Keshdan olindi\n";
}

print_r($user);

// Bir nechta foydalanuvchilarni keshlash
$users = [
    'user_1' => ['name' => 'Ali', 'age' => 25],
    'user_2' => ['name' => 'Vali', 'age' => 30],
    'user_3' => ['name' => 'Sardor', 'age' => 28]
];

$cache->setMultiple($users, 7200); // 2 soat

// Barcha foydalanuvchilarni olish
$cachedUsers = $cache->getMultiple(['user_1', 'user_2', 'user_3']);
print_r($cachedUsers);

// Ma'lum bir foydalanuvchini o'chirish
$cache->delete('user_1');

// Barcha keshni tozalash
// $cache->clear();