public function getErrors() { $jsonEntries = $this->redisClient->lrange($this->errorKey, 0, 99); $entries = []; foreach ($jsonEntries as $entry) { $entries[] = json_decode($entry, true); } return $entries; }
/** * @return array */ public function showAllPosts() { $key = "global:timeline"; $posts = $this->redisClient->lrange($key, 0, 10); $postData = []; foreach ($posts as $post) { $postData[] = $this->redisClient->get("post:{$post}"); } return $postData; }
public function getEventsFor($id) { if (!$this->predis->exists('events:' . $id)) { throw new AggregateDoesNotExist($id); } $serializedEvents = $this->predis->lrange('events:' . $id, 0, -1); $eventStream = []; foreach ($serializedEvents as $serializedEvent) { $eventData = $this->serializer->deserialize($serializedEvent, 'array', 'json'); $eventStream[] = $this->serializer->deserialize($eventData['data'], $eventData['type'], 'json'); } return new EventStream($id, $eventStream); }
/** * Peek for messages * * @param integer $limit * @return array Messages or empty array if no messages were present */ public function peek($limit = 1) { $result = $this->client->lrange("queue:{$this->name}:messages", -$limit, -1); if (is_array($result) && count($result) > 0) { $messages = []; foreach ($result as $value) { $message = $this->decodeMessage($value); // The message is still published and should not be processed! $message->setState(\Flowpack\JobQueue\Common\Queue\Message::STATE_SUBMITTED); $messages[] = $message; } return $messages; } return []; }
/** * Checks whether a key exists and, optionally, whether it has a given $value * * @param string $key The key name * @param mixed $value Optional. If specified, also checks the key has this * value. Booleans will be converted to 1 and 0 (even inside arrays) * * @return bool */ private function checkKeyExists($key, $value = null) { $type = $this->driver->type($key); if (is_null($value)) { return $type != 'none'; } $value = $this->boolToString($value); switch ($type) { case 'string': $reply = $this->driver->get($key); // Allow non strict equality (2 equals '2') $result = $reply == $value; break; case 'list': $reply = $this->driver->lrange($key, 0, -1); // Check both arrays have the same key/value pairs + same order $result = $reply === $value; break; case 'set': $reply = $this->driver->smembers($key); // Only check both arrays have the same values sort($reply); sort($value); $result = $reply === $value; break; case 'zset': $reply = $this->driver->zrange($key, 0, -1, 'WITHSCORES'); // Check both arrays have the same key/value pairs + same order $reply = $this->scoresToFloat($reply); $value = $this->scoresToFloat($value); $result = $reply === $value; break; case 'hash': $reply = $this->driver->hgetall($key); // Only check both arrays have the same key/value pairs (==) $result = $reply == $value; break; default: $result = false; } return $result; }
/** * Returns an array of pending commands on the destination queue. * If the destination is <code>null</code> commands from the local node queue are returned. * * @param string|null $destination * @return array */ public function getPendingCommands($destination = null) { $destination = $destination ? $destination : $this->nodeName; return $this->client->lrange(sprintf(self::COMMAND_QUEUE_KEY, $destination), 0, -1); }
$app->get('/', function () use($app) { return $app['twig']->render('index.html.twig', array('meetups' => $app['meetup']->getEvents())); })->bind('homepage'); // Specific event $app->get('/event/{id}', function ($id) use($app) { $event = $app['meetup']->getEvent($id); $client = new Client(); $checkins = array_filter($client->lrange('checkin_' . $id, 0, 300)); $winners = $app['random']->getRandomNumbers(0, count($checkins) - 1); return $app['twig']->render('event.html.twig', array('event' => $event, 'winners' => $winners, 'checkins' => $checkins)); })->bind('event'); // Check-in page for Event $app->get('/event/{id}/checkin', function ($id, Request $request) use($app) { $event = $app['meetup']->getEvent($id); $client = new Client(); $checkins = array_filter($client->lrange('checkin_' . $id, 0, 300)); return $app['twig']->render('event_checkin.html.twig', array('event' => $event, 'checkins' => $checkins)); })->bind('event_checkin'); // Checks a user into an event $app->post('/user/checkin', function (Request $request) use($app) { $userId = $request->get('user_id'); $eventId = $request->get('event_id'); $client = new Client(); $client->lpush('checkin_' . $eventId, $userId); return new Response(json_encode(array('result' => 'ok')), 200, array('Content-Type' => 'application/json')); })->bind('user_checkin'); // Error page $app->error(function (\Exception $e, $code) use($app) { if ($app['debug']) { return; }
<?php $loader = (require __DIR__ . '/../vendor/autoload.php'); $loader->add('Activity', __DIR__); use Predis\Client; use TylerKing\Activity\Broadcast; use TylerKing\Activity\Feed; use Activity\Status; use Activity\PhotoLike; $redis = new Client(); // Setup some friends and the current user (you can use a database for this). $redis->del('uid:1:friends'); $redis->del('feed:2'); $redis->lpush('uid:1:friends', 2); $redis->lpush('uid:1:friends', 3); $friends = $redis->lrange('uid:1:friends', 0, -1); // Make a status. $status = new Status($redis); $status->setCreator(1); $status->setStatus('Hey, I just joined this site!'); // Broadcast status to user's friends... $broadcast = new Broadcast($redis); $broadcast->setActivity($status); $broadcast->setTo($friends); $broadcast->flush(); // Make a photo like. $like = new PhotoLike($redis); $like->setCreator(1); $like->setPhotoId(5); $like->setPhoto('http://i.imgur.com/pcS7V.jpg'); // Broadcast photo like to user's friends...
/** * 获取列表数据 * 默认取出全部 * @param string $key * @param int $start 起始位置 * @param int $stop 结束位置 * @return array */ public function getList($key, $start = 0, $stop = -1) { // list:magazine:100:images 12 13 14 return $this->redis->lrange($key, $start, $stop); }