123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace Redis\Logger;
- class Reader {
-
- protected $redis;
-
- protected $settings;
-
- protected $templateCache = NULL;
-
- public function __construct(\Redis $redis, Settings $settings = NULL) {
- $this->redis = $redis;
- if (!isset($settings)) {
- $settings = new Settings();
- }
- $this->settings = $settings;
- }
-
- public function clear() {
- $this->templateCache = NULL;
- $this->redis->flushDB();
- }
-
- protected function ensureTemplateCache() {
- if (!isset($this->templateCache)) {
- $this->templateCache = $this->scan($this->settings->getRedisPattern(), 0, $this->settings->getScanBatchSize());
- }
- }
-
- public function getChannels() {
- $this->ensureTemplateCache();
- $channels = array();
- foreach ($this->templateCache as $key) {
- preg_match($this->settings->getChannelRegex(), $key, $matches);
- $channel = $matches[1];
- $channels[$channel] = $channel;
- }
- ksort($channels);
- return $channels;
- }
-
- public function getEntries($template, $skip = 0, $entries_per_page = 0) {
- $entries_per_page = $this->settings->getEntriesPerPage($entries_per_page);
-
- $start = $skip * $entries_per_page;
- $end = $start + $entries_per_page;
- $entries = $this->redis->lRange($template, $start, $end);
- return $entries;
- }
-
- protected function passesFilter($criterium, $regex, $string) {
- if (!isset($criterium)) {
- $ret = TRUE;
- }
- else {
- $sts = preg_match($regex, $string, $matches);
- $ret = $sts && ($matches[1] == $criterium);
- }
- return $ret;
- }
-
- public function getSettings() {
- return $this->settings;
- }
- public static function getTemplateChannel($template) {
- }
-
- public function getTemplates($channel = NULL, $severity = NULL, $skip = 0, $entries_per_page = 0) {
- $this->ensureTemplateCache();
- $matches = array();
- $count = 0;
- $channelRegex = $this->settings->getChannelRegex();
- $severityRegex = $this->settings->getSeverityRegex();
- $entries_per_page = $this->settings->getEntriesPerPage($entries_per_page);
- $start = $skip * $entries_per_page;
- $end = $start + $entries_per_page;
- foreach ($this->templateCache as $template) {
- if (!$this->passesFilter($channel, $channelRegex, $template)) {
- continue;
- }
- if (!$this->passesFilter($severity, $severityRegex, $template)) {
- continue;
- }
- $count++;
- if ($count >= $skip) {
- if ($count >= $end) {
- break;
- }
- $matches[] = $template;
- }
- }
- return $matches;
- }
-
- public function scan($pattern = '*', $max = 0, $batch_suggestion = 10) {
- $redis = $this->redis;
- $saved_scan_option = $redis->getOption(\Redis::OPT_SCAN);
- $redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
- $ret = array();
-
- $scan_it = NULL;
- $count = 0;
- $request_count = 1;
-
- while ($arr_keys = $redis->scan($scan_it, $pattern, $batch_suggestion)) {
- $request_count++;
- foreach ($arr_keys as $str_key) {
- $ret[] = $str_key;
- if ($max) {
- $count++;
- if ($count >= $max) {
- break 2;
- }
- }
- }
- }
- if ($saved_scan_option != \Redis::SCAN_RETRY) {
- $redis->setOption(\Redis::OPT_SCAN, $saved_scan_option);
- }
- return $ret;
- }
- }
|