/** * {@inheritdoc} */ public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null) { if (!$this->redis->llen($this->key)) { return 0; } if (!$transport->isStarted()) { $transport->start(); } $failedRecipients = (array) $failedRecipients; $count = 0; $time = time(); while ($message = unserialize($this->redis->lpop($this->key))) { $count += $transport->send($message, $failedRecipients); if ($this->getMessageLimit() && $count >= $this->getMessageLimit()) { break; } if ($this->getTimeLimit() && time() - $time >= $this->getTimeLimit()) { break; } } return $count; }
public function testltrim() { $this->redis->del('list'); $this->redis->lPush('list', 'val'); $this->redis->lPush('list', 'val2'); $this->redis->lPush('list', 'val3'); $this->redis->lPush('list', 'val4'); $this->assertEquals(TRUE, $this->redis->ltrim('list', 0, 2)); $this->assertEquals(3, $this->redis->llen('list')); $this->redis->ltrim('list', 0, 0); $this->assertEquals(1, $this->redis->llen('list')); $this->assertEquals('val4', $this->redis->lPop('list')); $this->assertEquals(TRUE, $this->redis->ltrim('list', 10, 10000)); $this->assertEquals(TRUE, $this->redis->ltrim('list', 10000, 10)); // test invalid type $this->redis->set('list', 'not a list...'); $this->assertEquals(FALSE, $this->redis->ltrim('list', 0, 2)); }
$log->set_logfile("/var/log/emoncms.log"); $log->set_topic("FEEDWRITER"); $log->info("Starting feedwriter process"); // Connect to redis $redis = new Redis(); while (!$redis->connect("127.0.0.1")) { sleep(1); $log->warn("Could not connect to redis, retrying"); } require "Modules/feed/engine/PHPTimeSeries.php"; require "Modules/feed/engine/PHPFina.php"; $engine = array(); $engine[Engine::PHPTIMESERIES] = new PHPTimeSeries($feed_settings['phptimeseries']); $engine[Engine::PHPFINA] = new PHPFina($feed_settings['phpfina']); while (true) { $len = $redis->llen("feedbuffer"); for ($i = 0; $i < $len; $i++) { $f = explode(",", $redis->lpop("feedbuffer")); $feedid = $f[0]; $timestamp = $f[1]; $value = $f[2]; $padding_mode = (int) $f[3]; $e = $redis->hget("feed:{$feedid}", 'engine'); if ($padding_mode == 1) { $engine[Engine::PHPFINA]->padding_mode = 'join'; } $engine[$e]->prepare($feedid, $timestamp, $value); $engine[Engine::PHPFINA]->padding_mode = 'nan'; } $log->info("PHPTimeSeries bytes written: " . $engine[Engine::PHPTIMESERIES]->save()); $log->info("PHPFina bytes written: " . $engine[Engine::PHPFINA]->save());
public function llen($key) { return parent::llen($this->generateUniqueKey($key)); }
<?php define('EMONCMS_EXEC', 1); require "route.php"; $route = new Route(get('q')); $redis = new Redis(); $redis->connect("127.0.0.1"); if ($route->format == 'json') { if ($route->controller == 'input') { if ($route->action == 'post') { $array = array("time" => time(), "apikey" => get('apikey'), "nodeid" => (int) get('node'), "csv" => get('csv')); $msg = json_encode($array); $buflength = $redis->llen('buffer'); if ($buflength < 1000) { $redis->rpush('buffer', $msg); } } } } function get($index) { $val = null; if (isset($_GET[$index])) { $val = $_GET[$index]; } return $val; }
if (!$success) { error_log("\r\n track_log redis is fail", 3, '/tmp/err_track_log.log'); echo "track_log redis is fail"; exit; } if ($rpassword) { $redis_handle->auth($rpassword); } $redis_handle->select($rdb); $redis_bi_key = 'bi'; while (true) { if ($to_exit) { echo "[" . date('Y-m-d H:i:s') . "] track log quit for user end\n"; break; } if ($redis_handle->llen($redis_bi_key) > 0) { $url = $redis_handle->rpop($redis_bi_key); if (!empty($url)) { $result = curl($url); if ($result === false) { $redis_handle->lpush($redis_bi_key, $url); } } } //usleep(100000); usleep(1); } /** * * @param string $url * @param array $post_data
<?php $redis = new Redis(); $redis->connect("127.0.0.1"); $i = 0; while (true) { if (time() - $ltime > 1) { $ltime = time(); print $i . "\n"; $i = 0; } if ($redis->llen('myqueue') > 0) { // check if there is an item in the queue to process $line_str = $redis->lpop('myqueue'); $i++; } }
$redis->connect("127.0.0.1"); require "Modules/user/user_model.php"; $user = new User($mysqli, $redis, null); require "Modules/feed/feed_model.php"; $feed = new Feed($mysqli, $redis, $timestore_adminkey); require "Modules/input/input_model.php"; $input = new Input($mysqli, $redis, $feed); require "Modules/input/process_model.php"; $process = new Process($mysqli, $input, $feed); $rn = 0; $ltime = time(); $usleep = 100000; while (true) { if (time() - $ltime >= 1) { $ltime = time(); $buflength = $redis->llen('buffer'); // A basic throthler to stop the script using up cpu when there is nothing to do. // Fine tune sleep if ($buflength < 2) { $usleep += 50; } else { $usleep -= 50; } // if there is a big buffer reduce sleep to zero to clear buffer. if ($buflength > 100) { $usleep = 0; } // if throughput is low then increase sleep significantly if ($rn == 0) { $usleep = 100000; }
<?php /* * 生成新消息,放入主queue中 */ include_once __DIR__ . '/config.php'; $redis = new Redis(); $redis->connect(REDIS_HOST, REDIS_PORT); while (1) { echo "Primary: " . $redis->llen(PRIMARY_CHANNEL) . ' Secondary: ' . $redis->llen(SECONDARY_CHANNEL) . "\n"; sleep(1); }
public static function make_toolbar() { $content = ''; $infowindows = ''; ob_start(); if (!empty(self::$links)) { echo '<ul class="panel links">'; foreach (self::$links as $link) { echo '<li>' . $link['label'] . ': <a href="' . $link['url'] . '">' . $link['link_text'] . '</a></li>'; } echo '</ul>'; } $stats = \jmvc\Db::$stats; if (is_array($stats)) { $total = $stats['select'] + $stats['insert'] + $stats['update'] + $stats['delete']; echo '<div class="panel"> <h3>Database Stats</h3> <table class="data"> <tr> <td><strong>Total</strong></td> <td class="num"><strong>' . $total . '</strong></td> </tr> <tr> <td>Select</td> <td class="num">' . $stats['select'] . '</td> </tr> <tr> <td>Insert</td> <td class="num">' . $stats['insert'] . '</td> </tr> <tr> <td>Update</td> <td class="num">' . $stats['update'] . '</td> </tr> <tr> <td>Delete</td> <td class="num">' . $stats['delete'] . '</td> </tr> </table>'; $queries = \jmvc\Db::$queries; if (is_array($queries) && !empty($queries)) { $rows = ''; foreach ($queries as $query) { $rows .= '<tr> <td class="num">' . round($query['time'] * 1000) . 'ms</td> <td>' . self::table_names($query['query']) . ' </td> <td><a href="#" class="showquery">Show Query</a> <div class="query">' . nl2br($query['query']) . '</div></td> </tr>'; } $infoWindows .= '<div id="jmvc-debug-dbqueries"> <table class="data"> ' . $rows . ' </table> </div>'; echo '<a href="#" rel="jmvc-debug-dbqueries" class="jmvc-debug-infoWindowLink">Show DB Queries</a>'; } echo '</div>'; } $stats = \jmvc\classes\Cache_Interface::$stats; if (is_array($stats)) { echo '<div class="panel"> <h3>Cache Stats</h3> <table class="data"> <tr> <td>Hits</td> <td class="num">' . $stats['hits'] . '</td> </tr> <tr> <td>Misses</td> <td class="num">' . $stats['misses'] . '</td> </tr> <tr> <td>Writes</td> <td class="num">' . $stats['writes'] . '</td> </tr> </table>'; if (!empty($stats['keys'])) { $rows = ''; foreach ($stats['keys'] as $key) { $rows .= '<tr> <td>' . $key[0] . '</td> <td>' . $key[1] . '</td> </tr>'; } $infoWindows .= '<div id="jmvc-debug-cache-keys"> <table class="data"> ' . $rows . ' </table> </div>'; echo '<a href="#" rel="jmvc-debug-cache-keys" class="jmvc-debug-infoWindowLink">Show Keys</a>'; } echo '</div>'; } if (isset($GLOBALS['_CONFIG']['redis'])) { $r = new \Redis(); $r->connect($GLOBALS['_CONFIG']['redis']['host'], $GLOBALS['_CONFIG']['redis']['port']); $mail_count = $r->llen('jmvc:rmail'); if (IS_PRODUCTION && $mail_count) { $encoded_message = $r->lindex('jmvc:rmail', 0); if ($encoded_message) { $message = json_decode($encoded_message); if (time() - $message->created > 1800) { \jmvc::notify_admin(new \Exception('Mail queue: stale message from ' . date('r', $message->created))); } } } $jobs_count = $r->llen('JMVC:jobs:low') + $r->llen('JMVC:jobs:high'); if (IS_PRODUCTION && $jobs_count) { $encoded_message = $r->lindex('JMVC:jobs:high', 0); if (!$encoded_message) { $encoded_message = $r->lindex('JMVC:jobs:low', 0); } if ($encoded_message) { $message = json_decode($encoded_message); if (time() - $message->created > 1800) { \jmvc::notify_admin(new \Exception('Job queue: stale job from ' . date('r', $message->created))); } } } } $end = array_pop(\jmvc::$traces); \jmvc::$traces[] = $end; $rows = ''; foreach (\jmvc::$traces as $trace) { $rows .= '<tr> <td class="num">' . round($trace['time']) . 'ms</td> <td>' . $trace['message'] . '</td> </tr>'; } $infoWindows .= '<div id="jmvc-debug-traces"> <table class="data"> ' . $rows . ' </table> </div>'; $content = ob_get_clean(); $display = isset($_COOKIE['jmvc-debug-toolbar']) ? '' : 'style="display:none;"'; return '<div id="jmvc-debug-container"> <div id="jmvc-debug-toolbar" ' . $display . '> ' . $content . ' <ul class="panel"> <li class="jmvc-debug-toggle-option" id="jmvc-bust-cache">Cache Buster</li> <li><a href="#" rel="jmvc-debug-traces" class="jmvc-debug-infoWindowLink">' . round($end['time']) . 'ms</a></li> <li>' . ($mail_count ?: 0) . ' unsent emails</li> <li>' . ($jobs_count ?: 0) . ' pending jobs</li> </ul> <div style="clear: both"></div> </div> <div class="jmvc-debug-toggle">X</div> </div> <div id="jmvc-debug-infoWindows">' . $infoWindows . '</div> <script type="text/javascript" src="/js/debug.js"></script> '; }
<?php header('Content-Type: text/event-stream;charset=UTF-8'); header('Cache-Control: no-cache'); header("Connection: keep-alive"); set_time_limit(0); $objRedis = new Redis(); $objRedis->connect("localhost"); $numStateEvents = $objRedis->llen('eventQueue'); for ($i = 0; $i < $numStateEvents; $i++) { $objRedis->lpop('eventQueue'); } while (true) { $event = $objRedis->lpop('eventQueue'); if ($event) { echo "data: {$event}\n\n"; ob_flush(); flush(); } }
/** * 设置某个商品在购物车里的数量 * 此项操作必须是购物车至少有一件的情况 */ public function cartSetCount() { if (Auth::check()) { $cartkey = Auth::user()->front_uid; } else { $cartkey = $this->getIP(); } $key = 'laravel:user:'******':cart'; $good_id = Input::get('good_id'); $shop_id = Input::get('shop_id'); // 不用 $count = Input::get('count'); $ids = array_count_values(Redis::lrange($key, 1, -1)); $num = $count - $ids[$good_id]; if ($num > 0) { for ($i = $num; $i > 0; $i--) { Redis::rpush($key, $good_id); } } elseif ($num < 0) { Redis::lrem($key, $num, $good_id); if (Redis::llen($key) == 1) { Redis::del($key); } } // 相等就不作处理了s //var_dump(Redis::lrange($key, 0, -1)); return Response::json(array('success' => 'true')); }