ugurcsen / phpeasycache
1.0.0
2020-09-13 22:38 UTC
Requires
- ext-json: *
This package is not auto-updated.
Last update: 2024-09-24 17:35:56 UTC
README
需求
PHP >= 7.4
安装
composer require ugurcsen/phpeasycache
或
<?php require 'path/to/php-easy-cache/easycache.php';
配置
<?php use EasyCache\Cache; $cache = new Cache(); $cache->saveLocation = '/var/temp'; //Should be changed for operating system but not necessary
缓存一些内容
<?php use EasyCache\CacheElement; $data = "Something which needs to cached."; $dataCache = $cache->createElement('data1', 540);// Key , ExpiresTime(seconds) $dataCache->save($data);//If data caching before expires date, Nothing will save
从缓存中获取内容
<?php $cachedData = $dataCache->get();//Getting cached data if($cachedData != null){ echo $cachedData; }
或
<?php if($dataCache->check()){//Checking cached data is exsist echo $dataCache->get(); }
删除缓存的内容
<?php $dataCache->clear();//Deleting cached data
完整示例
<?php use EasyCache\Cache; use EasyCache\CacheElement; $cache = new Cache(); $data = "Something which needs to cached."; $dataCache = $cache->createElement('data1', 540); $dataCache->save($data);//If data caching before expires date, Nothing will save if($dataCache->check()){ echo $dataCache->get(); } $dataCache->clear();
不定义元素进行创建、获取和删除
<?php use EasyCache\Cache; $cache = new Cache(); $data = "Something which needs to cached."; //Creating $cache->set('data1', 540, $data);//If data caching before expires date, Nothing will save //Getting and writing if($cache->checkWithKey('data1')){ echo $cache->getWithKey('data1'); } //Deleting $cache->clearWithKey('data1');