Some development considerations and redis configuration of phalcon

Development Considerations

  • Regarding the namespace, you need to load the namespace in the loader. For example, in the services directory, there is an Auth class.

·loader.php :

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

  • Add namespace to Auth class

namespace App\Service

  • import namespace

use App\Service\Auth

  • Redis configuration

·services.php

` $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; } );

`

·config.php

'redis' => [ 'prefix' => '', "host" => "127.0.0.1", "port" => 6379, 'lifetime'=>172800, "auth" => "", ],·/services/useRedis.php Use redis reference class

` class UseRedis {

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);
}

} `

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325148526&siteId=291194637