当前位置: 首页 > 工具软件 > phpFastCache > 使用案例 >

ci框架安装phpFastCache

哈骞仕
2023-12-01
  1. 首先,我们要编写处在项目根目录下的composer.json文件:
    {
    “require”: {
    “phpfastcache/phpfastcache”: “^6.0”
    }
    }

  2. 然后ctrl+R 输入cmd 打开黑窗口 composer update

  3. 安装完成之后创建一个Reviews.php文件:

     	// 不管使用什么方法安装phpFastCache都需要引入autoload.php文件,文件所放位置以及路径根据项目情况自行更改。
     	
     	require_once './vendor/autoload.php';
     	defined('BASEPATH') OR exit('No direct script access allowed');
     	use phpFastCache\CacheManager;
    
    
     	class Reviews extends CI_Controller {
     		public function phpfastcache(){
     			$this->load->model("Reviews_model");
     
     			CacheManager::setDefaultConfig(array("path" => '/cache'));
     
     			// Get instance of files cache
     			$objFilesCache = CacheManager::getInstance('files');
     
     			$key = "welcome_message";
     			 
     			// Try to fetch cached item with "welcome_message" key
     			$CachedString = $objFilesCache->getItem($key);
     			 
     			if (is_null($CachedString->get()))
     			{
     			    // The cached entry doesn't exist
     			    $numberOfSeconds = 60;
     			    $CachedString->set("月月写的phpFastCache测试!")->expiresAfter($numberOfSeconds);
     			    $objFilesCache->save($CachedString);
     			 
     			    echo "Not in cache yet, we set it in cache and try to get it from cache!</br>";
     			    echo "The value of welcome_message:" . $CachedString->get();
     			}
     			else
     			{
     			    // The cached entry exists
     			    echo "Already in cache!</br>";
     			    echo "The value of welcome_message:" . $CachedString->get();
     			}
     		}
     	}
    
  4. 然后去浏览器输入http://url/Reviews/phpfastcache
    Not in cache yet, we set it in cache and try to get it from cache!
    The value of welcome_message:月月写的phpFastCache测试!

 类似资料: