首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有编写器的情况下使用phpFastCache缓存查询?

如何在没有编写器的情况下使用phpFastCache缓存查询?
EN

Stack Overflow用户
提问于 2018-05-18 00:43:45
回答 1查看 1.4K关注 0票数 0

我试图使用phpFastCache来满足所有的缓存需求,但我真的不明白如何使用它。我理解他们的例子,是的,我已经尝试过,他们是成功的,但在我需要做的事情上,这对我没有帮助。

我正在尝试缓存一个查询(确切地说,是阀门的源查询协议)。

下面是结果,另外,我使用了一个单独的SourceQuery脚本,这就是结果(queryresults.php):

代码语言:javascript
复制
$serveroneip = "example.ip";
$serveroneport = "27015"
$server = new SourceQuery($serveroneip, $serveroneport);
$infos  = $server->getInfos();

然后将其添加到index.php页面:

代码语言:javascript
复制
<?php
include ("queryresults.php")
?>
<p>'.$infos['players'].' / '.$infos['places'].'</p>

这将只打印当前播放机计数和源服务器上的玩家总数。我基本上是在尝试缓存该查询,因为它有助于页面加载时间。

如果我听起来像个彻头彻尾的菜鸟,我很抱歉。这只是过去几天来一直令我沮丧的一个问题,我把这看作是最后的手段。如果你需要更多的信息,我可以很高兴地提供它!非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-19 13:11:18

在Phpfastcache V5中,库遵循PSR6接口。

因此,从根本上说,代码非常简单,而且对于composer来说甚至更容易:

代码语言:javascript
复制
composer require phpfastcache/phpfastcache

如果没有在全球安装:

代码语言:javascript
复制
php composer.phar require phpfastcache/phpfastcache

composer.phar可以在这里下载:https://getcomposer.org/composer.phar

下面是代码,在您的案例中:

代码语言:javascript
复制
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
 */

不管怎么说,我强烈建议你利用作曲家。这将使您的依赖关系管理更容易,并允许您获得对自动更新,冲突管理,自动噩梦等绝对控制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50402272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档