示例#1
0
 public function autoAction()
 {
     //注意:需要安装yac扩展,用于存储共享变量,下面的实例作为高并发计数器
     $yac = new Yac();
     $count = $yac->get('zqf');
     if (!$count) {
         $yac->set('zqf', 1);
     } else {
         $yac->set('zqf', $count + 0.5);
     }
     echo $count;
     exit;
 }
 private function cacheSet($cache_key, $val, $cache_time = null)
 {
     if (!$cache_time) {
         if (date('H') > 19 || date('H') < 3) {
             $cache_time = 60;
         } else {
             $cache_time = 5;
         }
     }
     if (class_exists('Yac')) {
         $yac = new \Yac();
         $yac->set($cache_key, $val, $cache_time);
     } else {
         if (function_exists('apc_fetch')) {
             apc_store($cache_key, $val, $cache_time);
         }
     }
 }
示例#3
0
<?php

// MurmurHash :
// 双散列法 :
// 共享内存 :
//
$yac = new Yac();
for ($i = 0; $i < 1000; $i++) {
    $key = "xxx" . rand(1, 10000);
    $value = str_repeat("x", rand(1, 10000));
    if (!$yac->set($key, $value)) {
        var_dump("write " . $i);
    }
    if ($value != ($new = $yac->get($key))) {
        var_dump("read " . $i);
    }
}
var_dump($i);
示例#4
0
 function doWork($param)
 {
     //process you logical 业务实际处理代码仍这里
     //return the result 使用return返回处理结果
     // array (
     //   'type' => 'SSS',
     //   'guid' => 'b3c29d615fc233a8780f5160bf3e059b',
     //   'fd' => 1,
     //   'api' =>
     //   array (
     //     'name' => 'abc',
     //     'param' =>
     //     array (
     //       0 => 234,
     //       1 => 99,
     //       ),
     //     ),
     //   )
     // Two route Controller : sync and async
     $routePrefix = '';
     $type = $param['type'];
     $apiName = $param['api']['name'];
     if ($type == DoraRPC\DoraConst::SW_SYNC_SINGLE || $type == DoraRPC\DoraConst::SW_SYNC_MULTI) {
         $routePrefix = 'sync/' . $apiName;
     } elseif ($type == DoraRPC\DoraConst::SW_ASYNC_SINGLE || $type == DoraRPC\DoraConst::SW_ASYNC_MULTI) {
         $routePrefix = 'async/' . $apiName;
     }
     // use prefix special every Interface ,prefix maxLength?
     // return $param;
     $yacPrefix = $type . "_" . $apiName;
     $key = $param['guid'];
     $yac = new Yac($yacPrefix);
     // Wrong-----ttl set 1 seconds. Attension Please!!! IF server quit/restart, All Yac cache will flush !!
     // I'm so stupid, Waht time Task would be called is unknown, Just delete the cache after doAction would be fine!!
     // $yac->set($key,$param['api'],1);//wrong, save it for remember! 2015-12-30 shurufa
     $yac->set($key, $param['api']);
     $app = self::getAppInstance();
     $route = $routePrefix . '/' . $yacPrefix . '/' . $key;
     // var_dump($app);
     return $app->handle($route);
 }
示例#5
0
 public static function setArrayData($kvs, $ttl = 30)
 {
     $yac = new Yac();
     $yac->set($kvs, $ttl);
 }
示例#6
0
 public function get_recommend()
 {
     $result = [];
     //从php.ini获取是否使用yac
     //empty: 不是null且不是0
     if (!empty(ini_get('yac.enable'))) {
         $yac = new Yac("zw");
         $recommended = $yac->get("recommended_articles");
         if (empty($recommended)) {
             $recommended = $this->article->get_recommended_articles();
             $yac->set("recommended_articles", $recommended);
         }
         $banner = $yac->get('banner');
         if (empty($banner)) {
             $banner = $this->article->get_banner_articles();
             $yac->set('banner', $banner);
         }
     } else {
         $recommended = $this->article->get_recommended_articles();
         $banner = $this->article->get_banner_articles();
     }
     $result['recommend'] = $recommended;
     $result['banner'] = $banner;
     header("Access-Control-Allow-Origin: *");
     $this->output->set_content_type('application/json');
     $this->output->set_output(json_encode($result));
 }