Code Monkey home page Code Monkey logo

predis-commands's Introduction

predis-commands

Adds some Lua-based atomic commands to Predis.

Motivation

Any individual Redis command is always atomic because Redis is single threaded. In some cases you may want to run several Redis commands atomically.

There are two common ways to achieve this:

  • Redis transactions. It utilizes MULTI/EXEC Redis commands to run some commands in sequence It also uses WATCH/DISCARD Redis commands for CAS optimistic concurrency.
  • Redis scripts. This one is transactional by definition: Redis uses the same Lua interpreter to run all the commands and guarantees that no other script or Redis command will be executed while a script is being executed.

This project uses Lua Scripting approach as it's usually both simpler and faster.

Installation

composer require angyvolin/predis-commands

What's included?

As of now, the predis-commands project extends Predis with following commands:

ZPOP - Removes and returns the top value in a zset, with its score.
ZRPOP - Removes and returns the bottom value in a zset, with its score.

How to use predis-commands ...

... in plain PHP

Code snippet below demonstrates how to use predis-commands regardless of any framework

<?php

require __DIR__.'/../vendor/autoload.php';

use Angyvolin\Predis\Command;

$client = new Predis\Client();
$client->getProfile()->defineCommand('zpop', Command\ZSetPop::class);
$client->getProfile()->defineCommand('zrpop', Command\ZSetReversePop::class);

... in Silex application

Code snippet below demonstrates integration with Silex application

<?php

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Angyvolin\Predis\Command;

class PredisCommandsServiceProvider implements ServiceProviderInterface
{
    public function register(Container $app)
    {
        $app->extend('redis', function ($redis) {
            /* @var \Predis\Client $redis */
            $redis->getProfile()->defineCommand('zpop', Command\ZSetPop::class);
            $redis->getProfile()->defineCommand('zrpop', Command\ZSetReversePop::class);
        
            return $redis;
        });
    }
}

Tests:

Tu run tests execute following command in a root directory of the predis-commands project:

phpunit

License

MIT License

predis-commands's People

Contributors

angyvolin avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.