Permalink
Browse files

Initial commit of Predis\Async.

The library is actually uber-alpha and in its first development stages
which means you should expect ugly spots and nasty bugs right around
the corner.

Contributions? More than welcome!
  • Loading branch information...
0 parents commit e2257a6a39e4367c3ac7792d45bb4b50d99548db @nrk committed May 19, 2012
@@ -0,0 +1,3 @@
+composer.phar
+vendor/
+experiments/
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Daniele Alessandri
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
+# Predis\Async #
+
+An asynchronous (non-blocking) version of [Predis](https://github.com/nrk/predis), the flexible and
+feature-complete PHP client library for the [Redis](http://redis.io) key-value store, built on top
+of [React](https://github.com/react-php) and using [phpiredis](https://github.com/seppo0010/phpiredis)
+to benefit from parsing the Redis protocol from inside a PHP extension.
+
+Predis\Async is currently __highly experimental__ which means that it is unstable, lacks various
+features and the API is ugly and not yet finalized as of now. The client foundation is being built
+on top of the evented loop abstraction offered by [React](https://github.com/react-php), a new event
+oriented framework for PHP under heavy-development that aims to provide everything needed to build
+reusable components and applications using an evented approach with non-blocking I/O.
+
+I would like to stress the fact that the code in Predis\Async is addmittedly still ugly and blatant
+bugs are most likely right around the corner, but feel free to open pull-requests with fixes or just
+[report them](https://github.com/nrk/predis-async/issues).
+
+Contributions to both Predis\Async and React are highly welcome and appreciated.
+
+## Installing ##
+
+Predis\Async is available on [Packagist](http://packagist.org/packages/predis/predis-async) and can
+be installed through [Composer](http://getcomposer.org/). Using it to your application is simply a
+matter of adding `"predis/predis-async": "dev-master"` in your `composer.json` list of `require`ed
+libraries. Also remember that you must have [phpiredis](https://github.com/seppo0010/phpiredis)
+pre-installed as a PHP extension.
+
+## Example ##
+
+``` php
+<?php
+require __DIR__.'/../autoload.php';
+
+use Predis\Async\Client as PredisAsync;
+use React\EventLoop\StreamSelectLoop as EventLoop;
+
+$listener = new PredisAsync('tcp://127.0.0.1:6379', $loop = new EventLoop());
+
+$listener->connect(function () use ($listener) {
+ echo "Connected to Redis, now listening for incoming messages...\n";
+
+ $logger = new PredisAsync('tcp://127.0.0.1:6379', $listener->getEventLoop());
+
+ $listener->subscribe('nrk:channel', function ($event) use ($logger) {
+ list(, $chan, $msg) = $event;
+
+ $logger->rpush("store:$chan", $msg, function () use ($chan, $msg) {
+ echo "Stored message `$msg` from $chan.\n";
+ });
+ });
+});
+
+$loop->run();
+```
+
+## Most immediate TODO list ##
+
+- Provide a better API that suits best the asynchronous model.
+- Add connection timeouts (we actually need timers to get into React\EventLoop first).
+- Add tests. Lots of them.
+- Everything else.
+
+## Contributing ##
+
+If you want to work on Predis\Async, it is highly recommended that you first run the test suite in
+order to check that everything is OK, and report strange behaviours or bugs. When modifying Predis
+please make sure that no warnings or notices are emitted by PHP by running the interpreter in your
+development environment with the `error_reporting` variable set to `E_ALL | E_STRICT`.
+
+The recommended way to contribute to Predis\Async is to fork the project on GitHub, create new topic
+branches on your newly created repository to fix or add features (possibly with tests covering your
+modifications) and then open a new pull request with a description of the applied changes. Obviously
+you can use any other Git hosting provider of your preference.
+
+## Dependencies ##
+
+- [PHP](http://www.php.net/) >= 5.3.2
+- [Predis](https://github.com/nrk/predis) (Git master branch)
+- [phpiredis](https://github.com/seppo0010/phpiredis) (Git master branch)
+- [React](https://github.com/react-php) (Git master branch)
+- [PHPUnit](http://www.phpunit.de/) >= 3.5.0 (needed to run the test suite)
+
+### Project ###
+- [Source code](http://github.com/nrk/predis-async/)
+- [Issue tracker](http://github.com/nrk/predis-async/issues)
+
+## Author ##
+
+- [Daniele Alessandri](mailto:suppakilla@gmail.com) ([twitter](http://twitter.com/JoL1hAHN))
+
+## License ##
+
+The code for Predis\Async is distributed under the terms of the MIT license (see LICENSE).
@@ -0,0 +1,2 @@
+0.0.1-dev
+
@@ -0,0 +1,12 @@
+<?php
+
+/*
+ * This file is part of the PredisAsync package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require __DIR__.'/vendor/autoload.php';
@@ -0,0 +1,24 @@
+{
+ "name": "predis/predis-async",
+ "type": "library",
+ "description": "Asynchronous version of Predis",
+ "keywords": ["nosql", "redis", "predis"],
+ "homepage": "http://github.com/nrk/predis-async",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "Daniele Alessandri",
+ "email": "suppakilla@gmail.com",
+ "homepage": "http://clorophilla.net"
+ }
+ ],
+ "require": {
+ "php": ">=5.3.0",
+ "predis/predis": "dev-master",
+ "react/event-loop": "dev-master",
+ "ext-phpiredis": "*"
+ },
+ "autoload": {
+ "psr-0": {"Predis\\Async": "lib/"}
+ }
+}
@@ -0,0 +1,98 @@
+<?php
+
+/*
+ * This file is part of the Predis\Async package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis\Async\Buffer;
+
+/**
+ * Simple string buffer class.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class StringBuffer
+{
+ private $buffer;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct()
+ {
+ $this->buffer = '';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function read($length)
+ {
+ if (false === $buffer = substr($this->buffer, 0, $length)) {
+ return '';
+ }
+
+ return $buffer;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function consume($length)
+ {
+ if ('' !== $buffer = $this->read($length)) {
+ $this->buffer = substr($this->buffer, strlen($buffer)) ?: '';
+ }
+
+ return $buffer;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function discard($length)
+ {
+ $this->buffer = substr($this->buffer, $length) ?: '';
+
+ return $length;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function append($buffer)
+ {
+ $this->buffer .= $buffer;
+
+ return strlen($buffer);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isEmpty()
+ {
+ return $this->buffer === '';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function length()
+ {
+ return strlen($this->buffer);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function reset()
+ {
+ $this->buffer = '';
+ }
+}
Oops, something went wrong.

0 comments on commit e2257a6

Please sign in to comment.