如何在没有 Composer 的情况下使用 phpFastCache 缓存查询?
How to cache a query with phpFastCache without Composer?
我正在尝试使用 phpFastCache 来满足所有缓存需求,但我真的不明白如何使用它。我了解他们的例子,是的,我已经尝试过,他们是成功的,但它对我需要做的事情没有帮助。
我正在尝试缓存查询(确切地说是 Valve 的源查询协议。)
这是结果,另外,我使用的是单独的 SourceQuery 脚本,这只是结果 (queryresults.php):
$serveroneip = "example.ip";
$serveroneport = "27015"
$server = new SourceQuery($serveroneip, $serveroneport);
$infos = $server->getInfos();
然后将其添加到 index.php 页面:
<?php
include ("queryresults.php")
?>
<p>'.$infos['players'].' / '.$infos['places'].'</p>
这只会打印当前玩家数量和源服务器上的玩家总数。我基本上是在尝试缓存该查询,因为它有助于页面加载时间。
如果我在这方面听起来像个菜鸟,我很抱歉。这只是过去几天让我感到沮丧的一个问题,我把这里作为最后的手段。如果您需要更多信息,我很乐意提供!非常感谢您的帮助!
自 Phpfastcache V5 起,库符合 PSR6 接口
所以基本上代码会非常简单,使用 composer 会更简单:
composer require phpfastcache/phpfastcache
如果它没有全局安装:
php composer.phar require phpfastcache/phpfastcache
composer.phar可以在这里下载:https://getcomposer.org/composer.phar
现在是代码,你的案例:
use Phpfastcache\CacheManager;
/**
* You have two many ways...
* Via composer:
*/
require 'vendor/autoload.php';
/**
* Or if you have absolutely no choice, we provide a standalone autoloader
*/
// require 'phpfastcache/src/autoload.php';
/**
* We are using the default but most used driver: Files
* You can use redis/predis, etc but it's a bit more complexe
*/
$cachePool = CacheManager::getInstance('Files');
$cacheItem = $cachePool->getItem('mySteamServer');
/**
* Does we found something in cache ?
*/
if($cacheItem->isHit()){
/**
* Yes, let's use it
*/
$infos = $cacheItem->get();
}else{
/**
* Nahh, let's retrieve the server data and cache them
*/
$serveroneip = "example.ip";
$serveroneport = "27015";
$server = new SourceQuery($serveroneip, $serveroneport);
$infos = $server->getInfos();
$cacheItem->set($infos)->expiresAfter(300);// The TTL in seconds, here is 5 minutes
$cachePool->save($cacheItem);// Persist the cache item
}
/**
* Rest of your code goes here
*/
无论如何,我强烈建议您使用 composer。这将使您的依赖关系管理更容易,并让您获得对自动更新、冲突管理、自动加载噩梦等的绝对控制。
我正在尝试使用 phpFastCache 来满足所有缓存需求,但我真的不明白如何使用它。我了解他们的例子,是的,我已经尝试过,他们是成功的,但它对我需要做的事情没有帮助。
我正在尝试缓存查询(确切地说是 Valve 的源查询协议。)
这是结果,另外,我使用的是单独的 SourceQuery 脚本,这只是结果 (queryresults.php):
$serveroneip = "example.ip";
$serveroneport = "27015"
$server = new SourceQuery($serveroneip, $serveroneport);
$infos = $server->getInfos();
然后将其添加到 index.php 页面:
<?php
include ("queryresults.php")
?>
<p>'.$infos['players'].' / '.$infos['places'].'</p>
这只会打印当前玩家数量和源服务器上的玩家总数。我基本上是在尝试缓存该查询,因为它有助于页面加载时间。
如果我在这方面听起来像个菜鸟,我很抱歉。这只是过去几天让我感到沮丧的一个问题,我把这里作为最后的手段。如果您需要更多信息,我很乐意提供!非常感谢您的帮助!
自 Phpfastcache V5 起,库符合 PSR6 接口
所以基本上代码会非常简单,使用 composer 会更简单:
composer require phpfastcache/phpfastcache
如果它没有全局安装:
php composer.phar require phpfastcache/phpfastcache
composer.phar可以在这里下载:https://getcomposer.org/composer.phar
现在是代码,你的案例:
use Phpfastcache\CacheManager;
/**
* You have two many ways...
* Via composer:
*/
require 'vendor/autoload.php';
/**
* Or if you have absolutely no choice, we provide a standalone autoloader
*/
// require 'phpfastcache/src/autoload.php';
/**
* We are using the default but most used driver: Files
* You can use redis/predis, etc but it's a bit more complexe
*/
$cachePool = CacheManager::getInstance('Files');
$cacheItem = $cachePool->getItem('mySteamServer');
/**
* Does we found something in cache ?
*/
if($cacheItem->isHit()){
/**
* Yes, let's use it
*/
$infos = $cacheItem->get();
}else{
/**
* Nahh, let's retrieve the server data and cache them
*/
$serveroneip = "example.ip";
$serveroneport = "27015";
$server = new SourceQuery($serveroneip, $serveroneport);
$infos = $server->getInfos();
$cacheItem->set($infos)->expiresAfter(300);// The TTL in seconds, here is 5 minutes
$cachePool->save($cacheItem);// Persist the cache item
}
/**
* Rest of your code goes here
*/
无论如何,我强烈建议您使用 composer。这将使您的依赖关系管理更容易,并让您获得对自动更新、冲突管理、自动加载噩梦等的绝对控制。