phalcon 一些開發注意事項和redis配置

開發注意事項

  • 關於命名空間,須要在loader中加載命名空間,如 services目錄中,有Auth類。

·loader.php :php

'App\Service'=>'../app/services';redis

  • Auth類中添加命名空間

namespace App\Serviceapp

  • 引入命名空間

use App\Service\Auththis

  • Redis配置

·services.phpspa

` $di->setShared("redis", function(){ #$frontCache = new FrontendData(["lifetime" => 86400]); $cache = new \Redis(); $config = $this->getConfig(); $cache->connect( $config->redis->host, $config->redis->port ); return $cache; } );code

`開發

·config.phpget

'redis' => [ 'prefix' => '', "host" => "127.0.0.1", "port" => 6379, 'lifetime'=>172800, "auth" => "", ], ·/services/useRedis.php 使用redis的參考類it

` class UseRedis {io

protected $redis = null;

public function __construct($redis = null)
{
    $this->redis =$this->initRedis();
}

 protected function initRedis(){
    $di = \Phalcon\Di::getDefault();;
    $redis = $di['redis'];
    return $redis;
}

public function set($key , $value, $expire)
{
    $this->redis->setex($key,$expire,$value);


}
public function get($key)
{
    return $this->redis->get($key);
}

} `

相關文章
相關標籤/搜索