Skip to content

Commit

Permalink
cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hedeqiang committed Nov 19, 2019
1 parent c4d49d6 commit 38c4134
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/IM.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Hedeqiang\IM\Exceptions\HttpException;
use Hedeqiang\IM\Support\Config;
use Hedeqiang\IM\Traits\HasHttpRequest;
use Psr\SimpleCache\CacheInterface;
use Tencent\TLSSigAPIv2;
use Hyperf\Utils\ApplicationContext;

class IM
{
Expand Down Expand Up @@ -97,16 +99,29 @@ protected function buildEndpoint(string $servername, string $command): string
*/
protected function generateSign(string $identifier, int $expires = 15552000): string
{
$container = \Hyperf\Utils\ApplicationContext::getContainer();
$cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
$sign = $cache->get($identifier.'_cache');
if (!$sign) {
$cache = $this->di()->get(CacheInterface::class);

if (!$cache->has($identifier.'_cache'))
{
$api = new TLSSigAPIv2($this->config->get('sdk_app_id'), $this->config->get('secret_key'));
$sign = $api->genSig($identifier, $expires);
$cache->set($identifier.'_cache', $expires, $sign);
$cache->set($identifier.'_cache', $sign, $expires);
return $sign;
}
var_dump($sign);
return $cache->get($identifier.'_cache');
}

return $sign;
/**
* Finds an entry of the container by its identifier and returns it.
* @param null|mixed $id
* @return mixed|\Psr\Container\ContainerInterface
*/
protected function di($id = null)
{
$container = ApplicationContext::getContainer();
if ($id) {
return $container->get($id);
}
return $container;
}
}

0 comments on commit 38c4134

Please sign in to comment.