/**
  * 定时任务,将redis中的点击次数更新到mysql中
  */
 public function redisToMysql()
 {
     //>>1.连接上redis
     $redis = getRedis();
     //>>2.需要从redis中得到所有商品的浏览次数
     $keys = $redis->keys('goods_view_times:*');
     $values = $redis->mget($keys);
     //>>3.将浏览次数保存到数据库表goods_click中
     foreach ($keys as $i => $key) {
         $goods_id = str2arr($key, ':')[1];
         //从goods_view_times:10中取出商品的id
         $view_times = $values[$i];
         //对应的浏览次数
         $row = M('GoodsClick')->where('goods_id=' . $goods_id)->find();
         if ($row) {
             //有则更新
             M('GoodsClick')->where(array('goods_id' => $goods_id))->setInc('click_times', $view_times);
         } else {
             //无则生成
             M('GoodsClick')->add(array('goods_id' => $goods_id, 'click_times' => $view_times));
         }
     }
     //>>4.将redis中的键删除
     $redis->del($keys);
 }
Example #2
0
 public function clicknumRedisToMysql()
 {
     //>>1.从redis中取出商品的浏览次数
     $redis = getRedis();
     $result = $redis->zRange('goods_clicknum', 0, -1, true);
     //>>2.将商品的浏览次数同步的mysql中
     foreach ($result as $goods_id => $clicknum) {
         $goods_id = substr($goods_id, 12);
         $sql = "update goods set clicknum  = {$clicknum} where id = {$goods_id}";
         M()->execute($sql);
     }
 }
Example #3
0
function exec_xhprof_disable()
{
    $xhprof_data = xhprof_disable();
    $url = 'http://' . php_uname('n') . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
    if (!empty($_SERVER['QUERY_STRING'])) {
        $url .= '?' . urlencode($_SERVER['QUERY_STRING']);
    }
    $data = array('url' => $url, 'method' => $_SERVER['REQUEST_METHOD'], 'time' => $_SERVER['REQUEST_TIME'], 'xhprof_data' => $xhprof_data);
    global $config;
    global $redis_key;
    $Redis = getRedis($config);
    $Redis->zadd($redis_key, time(), serialize($data));
}
Example #4
0
/**
 * 返回redis到句柄
 * @param string $config 参数
 * @return Redis
 */
function getRedisEx($config = '')
{
    if (empty($config)) {
        return getRedis();
    }
    $redis_config = C($config);
    if (empty($redis_config)) {
        $redis_config = C('MIXED_CACHE');
        if (empty($redis_config)) {
            return getRedis();
        }
    }
    $redis_auth = isset($redis_config['REDIS_AUTH']) ? $redis_config['REDIS_AUTH'] : NULL;
    return getRedis($redis_config['REDIS_DB'], $redis_config['REDIS_HOST'], $redis_config['REDIS_PORT'], $redis_auth);
}
Example #5
0
 /**
  * 展示一个商品的详细
  */
 public function goods($id)
 {
     //        $goods = S('goods_'.$id);  //goods_xxx : xxxx是id的值
     //        if(!$goods){  //如果不在执行下面的代码进行查询..
     //>>1.根据id查询出当前商品的基本信息
     $goodsModel = D('Goods');
     $goods = $goodsModel->find($id);
     //>>2.查询出商品的简介内容... goods_intro
     $info = M('GoodsIntro')->getFieldByGoods_id($id, 'intro');
     $goods['content'] = $info;
     //>>3.查询出相册中的数据.. 只不过查询出的第一张图片是该商品的logo
     //>>3.1 先查询出当前商品对应的相册数据
     $gallerys = D('GoodsGallery')->getGalleryPath($id);
     //>>3.2 将logo放到相册的第一张图片上
     array_unshift($gallerys, $goods['logo']);
     $goods['gallerys'] = $gallerys;
     //>>4. 根据商品的分类id找到他的父分类
     $goodsCategoryModel = D('GoodsCategory');
     $parentGoodsCategorys = $goodsCategoryModel->getParent($goods['goods_category_id']);
     $goods['parentGoodsCategorys'] = $parentGoodsCategorys;
     //>>5. 查询出当前商品的属性
     if ($goods['stock_type'] == 2) {
         $goods['goodsAttributes'] = $goodsModel->getGoodsAttribute($id);
     }
     //再将当前商品缓存起来
     //            S('goods_'.$id,$goods);
     //        }
     $this->assign($goods);
     $this->assign('meta_title', '商品列表....' . $goods['name']);
     /**
      * 使用php的redis扩展来操作redis数据库中.
      */
     /**
      * 1.连接到redis中
      */
     $redis = getRedis();
     /**
      * 2.操作redis
      */
     $goods_clicknum = $redis->zIncrBy('goods_clicknum', 1, 'goods_views:' . $id);
     $this->display('goods');
 }
Example #6
0
    foreach ($follow_files as $k => $v) {
        if ($chan == "pubsub:" . $v["filename"]) {
            $new_hash = $msg_decode["hash"];
            if ($new_hash != $v["hash"]) {
                $new_mtime = $msg_decode["mtime"];
                send_event($redis, $v["event"], $new_mtime, $v["mtime"], $v["filename"]);
                $follow_files[$k]["hash"] = $new_hash;
                $follow_files[$k]["mtime"] = $new_mtime;
            }
            break;
        }
    }
}
// Predis allows only pubsub in single connection -> get separate connection for listening.
// Another one is required for updating statistics etc.
$pubredis = getRedis();
$pubsub = $pubredis->pubSub();
$pubsub->subscribe("pubsub:data:twitter.json", "pubsub:cache.manifest", "pubsub:" . $filename);
$counter = 1800;
while ($counter > 0) {
    foreach ($pubsub as $message) {
        switch ($message->kind) {
            case 'subscribe':
                break;
            case 'message':
                process_pubsub($redis, $message->channel, $message->payload);
                break;
        }
        $counter--;
        if ($counter == 0) {
            error_log("pubsub unsubscribe");
Example #7
0
<?php

require 'Predis/Autoloader.php';
Predis\Autoloader::register();
function getRedis()
{
    return new Predis\Client("unix:///home/redis/redis.sock");
}
if (!isset($redis)) {
    $redis = getRedis();
}
Example #8
0
/**
 * Save mail callback
 * Accepts an email received from a client during the DATA command.
 * This email is processed, the recipient host is verified, the body is
 * decoded, then saved to the database.
 * This is an example save mail function.
 * Of course, yours can be better! Eg. add buffered writes, place email on message queue, encryption, etc
 *
 * @param string $email
 * @param        $rcpt_to
 * @param        $helo
 * @param        $helo_ip
 *
 * @return array, with the following elements array($hash, $recipient)
 * where the $hash is a unique id for this email.
 */
function my_save_email($email, $rcpt_to, $helo, $helo_ip)
{
    global $GM_ERROR;
    $mimemail = null;
    $hash = '';
    $email .= "\r\n";
    list($to, $from, $subject) = get_email_headers($email, array('To', 'From', 'Subject'));
    $rcpt_to = extract_rcpt_email($rcpt_to);
    $from = extract_email($from);
    list($mail_user, $mail_host) = explode('@', $rcpt_to);
    if (is_host_allowed($mail_host)) {
        $db = get_db_link();
        if ($db === false) {
            // could not get a db connection
            $GM_ERROR = 'could not get a db connection';
            return array(false, false);
        }
        $to = $mail_user . '@' . GM_PRIMARY_MAIL_HOST;
        // change it to the primary host
        if (GSMTP_VERIFY_USERS) {
            // Here we can verify that the recipient is actually in the database.
            // Note that guerrillamail.com does not do this - you can send email
            // to a non-existing email address, and set to this email later.
            // just an example:
            if (array_pop(explode('@', $to)) !== 'sharklasers.com') {
                // check the user against our user database
                $user = array_shift(explode('@', $to));
                $sql = "SELECT * FROM `gm2_address` WHERE `address_email`=" . $db->quote($user . '@guerrillamailblock.com') . " ";
                $stmt = $db->query($sql);
                if ($stmt->rowCount() == 0) {
                    $GM_ERROR = 'could not verify user';
                    return false;
                    // no such address
                }
            }
        }
        $hash = md5($to . $from . $subject . microtime());
        // add 'received' headers
        $add_head = '';
        $add_head .= "Delivered-To: " . $to . "\r\n";
        $add_head .= "Received: from " . $helo . " (" . $helo . "  [" . $helo_ip . "])\r\n";
        $add_head .= "\tby " . GSMTP_HOST_NAME . " with SMTP id " . $hash . "@" . GSMTP_HOST_NAME . ";\r\n";
        $add_head .= "\t" . gmdate('r') . "\r\n";
        $email = $add_head . $email;
        $body = 'gzencode';
        $charset = '';
        $has_attach = '';
        $content_type = '';
        $email = gzcompress($email, 6);
        $redis = getRedis();
        if (is_object($redis)) {
            // send it of to Redis
            try {
                if ($redis->setex($hash, 3600, $email) === true) {
                    $body = 'redis';
                    $email = '';
                    //log_line("saved to redis $hash $to");
                } else {
                    log_line("save failed");
                }
            } catch (\RedisException $e) {
                log_line("redis exeption" . var_dump($e, true));
            }
        } else {
            log_line("no redis for you\n");
        }
        $sql = "INSERT INTO " . GM_MAIL_TABLE . " (\n                `date`,\n                `to`,\n                `from`,\n                `subject`,\n                `body`,\n                `charset`,\n                `mail`,\n                `spam_score`,\n                `hash`,\n                `content_type`,\n                `recipient`,\n                `has_attach`,\n                `ip_addr`\n            )\n            VALUES (\n                '" . gmdate('Y-m-d H:i:s') . "',\n                " . $db->quote($to) . ",\n                " . $db->quote($from) . ",\n                " . $db->quote($subject) . ",\n                " . $db->quote($body) . ",\n                " . $db->quote($charset) . ",\n                " . $db->quote($email) . ",\n                0 " . ",\n                " . $db->quote($hash) . ",\n                " . $db->quote($content_type) . ",\n                " . $db->quote($to) . ",\n                " . $db->quote($has_attach) . ",\n                " . $db->quote($helo_ip) . "\n            ) ";
        try {
            $db->query($sql);
            $id = $db->lastInsertId();
            if ($id) {
                $sql = "UPDATE\n                            gm2_setting\n                       SET\n                            `setting_value` = `setting_value`+1\n                       WHERE\n                            `setting_name`='received_emails'\n                            LIMIT 1";
                $db->query($sql);
            }
        } catch (\PDOException $e) {
            $GM_ERROR = 'save error ' . $e->getMessage();
            log_line('Failed to save email From:' . $from . ' To:' . $to . ' ' . $e->getMessage() . ' ' . $sql, 1);
        }
    } else {
        $GM_ERROR = " -{$mail_host}- not in allowed hosts:" . $mail_host . " ";
    }
    return array($hash, $to);
}
<?php

$data = array();
for ($j = 1; $j <= 20; $j++) {
    for ($h = 0; $h < 20; $h++) {
        $data[$j][$h] = $h;
    }
}
define("HOST_REDIS", '127.0.0.1');
$array_url = ['http://www.sanook.com', 'http://www.kapook.com', 'http://www.mthai.com', 'http://pantip.com', 'http://www.google.co.th'];
connectRedis();
excute('', $array_url);
dd(getRedis('data'));
function dd($data)
{
    echo "\r\n";
    var_dump($data);
    echo "\r\n";
}
function excute($url = '', $data)
{
    connectRedis();
    $response = array();
    $loop = count($data);
    for ($i = 0; $i < $loop; $i++) {
        # code...
        $pid = pcntl_fork();
        if (!$pid) {
            // sleep(1);
            print "In child {$i}\n";
            if (array_key_exists($i, $data)) {
Example #10
0
    </head>
<body>
<?php 
require_once dirname(__FILE__) . '/config/config.php';
require_once dirname(__FILE__) . '/lib/db.php';
date_default_timezone_set('Asia/Shanghai');
$pageCount = $page_count;
//计算偏移量
$page = $_GET['page'];
if (!is_numeric($page)) {
    $page = 1;
}
$start = ($page - 1) * $pageCount;
$end = $page * $pageCount - 1;
//查询数据
$Redis = getRedis($config);
$count = 0;
if ($Redis) {
    $count = $Redis->zCard($redis_key);
    $rows = $Redis->zRevRange($redis_key, $start, $end);
    //取最新的100个
}
//计算有多少页
$pageNum = ceil($count / $pageCount);
//格式化成数组
$list = array();
foreach ($rows as $v) {
    $item = unserialize($v);
    if ($item['method'] == 'GET') {
        $method_class = 'label-success';
    } else {
Example #11
0
function getTable($id)
{
    $redis = getRedis();
    $table = $redis->hgetAll("table:{$id}");
    //处理座位信息
    $seats = array();
    for ($i = 1; $i <= SEAT_MAX; ++$i) {
        $seatKey = "seat-{$i}";
        if (isset($table[$seatKey])) {
            $seats[$i] = json_decode($table[$seatKey], true);
            unset($table[$seatKey]);
        }
    }
    $table['seats'] = $seats;
    return $table;
}