123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class CachingNoticeStream extends NoticeStream
- {
- const CACHE_WINDOW = 200;
- public $stream = null;
- public $cachekey = null;
- public $useLast = true;
- function __construct($stream, $cachekey, $useLast = true)
- {
- $this->stream = $stream;
- $this->cachekey = $cachekey;
- $this->useLast = $useLast;
- }
- function getNoticeIds($offset, $limit, $sinceId, $maxId)
- {
- $cache = Cache::instance();
-
-
- if (empty($cache) ||
- $sinceId != 0 || $maxId != 0 ||
- is_null($limit) ||
- ($offset + $limit) > self::CACHE_WINDOW) {
- return $this->stream->getNoticeIds($offset, $limit, $sinceId, $maxId);
- }
-
- $idkey = Cache::key($this->cachekey);
- $idstr = $cache->get($idkey);
- if ($idstr !== false) {
-
- $window = explode(',', $idstr);
- $ids = array_slice($window, $offset, $limit);
- return $ids;
- }
- if ($this->useLast) {
-
-
-
-
-
- $laststr = $cache->get($idkey.';last');
- if ($laststr !== false) {
- $window = explode(',', $laststr);
- $last_id = $window[0];
- $new_ids = $this->stream->getNoticeIds(0, self::CACHE_WINDOW, $last_id, 0);
- $new_window = array_merge($new_ids, $window);
- $new_windowstr = implode(',', $new_window);
- $result = $cache->set($idkey, $new_windowstr);
- $result = $cache->set($idkey . ';last', $new_windowstr);
- $ids = array_slice($new_window, $offset, $limit);
- return $ids;
- }
- }
-
-
- $window = $this->stream->getNoticeIds(0, self::CACHE_WINDOW, 0, 0);
- $windowstr = implode(',', $window);
- $result = $cache->set($idkey, $windowstr);
- if ($this->useLast) {
- $result = $cache->set($idkey . ';last', $windowstr);
- }
-
- $ids = array_slice($window, $offset, $limit);
- return $ids;
- }
- }
|