EzDevInfo.com

predis

Flexible and feature-complete PHP client library for Redis Home · nrk/predis Wiki · GitHub predis - flexible and feature-complete php client library for redis

can predis hmget use array as parameter of multiple fields

can predis use array as 2nd parameter of hmget() to retrieve multiple fields on one go? e.g. $client->hmget($key, $fields); //$fields is an array

Can it also accept many parameters of string as fields? e.g.: $client->hmget($key, $field1, $field2, $field3);


Source: (StackOverflow)

How to user predis for publish more than one time

How can I publish info between the clients more than once?
I mean when I publish info from one user to other, he receives and backwards, but this is only once.
Because when one user send something to the other, the GET is being loaded and the receiving stops, how can I make it that way so the clients receives forever, not only once?


Source: (StackOverflow)

Advertisements

Is there a Predis documentation?

I was using phpredis and now I am also trying predis for my php applications, but I couldn't find a good documentation for the second one. There is a "How to use" in github, but I find it quite short. I checked the examples though and I noticed that they are using the Redis commands in "lower case" characters. I tried a few and it works, but I don't want to try them all to see if this is true...


Source: (StackOverflow)

Problems with using hmset in Predis

I have a problem with using $predis->hmset(). What parameters i need use? I try many variants, but without success.

$this->client()->hmset( $this->name, array( 1 => 3 ))

$this->client()->hmset( $this->name, array( 1, 3 ))

Source: (StackOverflow)

Predis sharding (consistent hashing)

Predis claim to have Client-side sharding (support for consistent hashing of keys). http://github.com/nrk/predis

I can do sharding using connect to an array of profiles (nodes) but it isn't consistent hashing. When I add another node to the pool, some of the keys can't be found. Anyone has any experience on this?

Using php 5.2 (and redis's php 5.2 version).


Source: (StackOverflow)

How do I install Predis on XAMPP windows?

I installed Predis on XAMPP windows machine using pearhub (pear install pearhub/predis). It installed without any error messages. But when I do the following

<?php
require "Predis.php";
$redis = new Predis/Client();
$redis->set('library', 'predis');
$value = $redis->get('library');
?>

It says Predis class not found. Any ideas how to properly install this on windows?


Source: (StackOverflow)

Why isn't my PHPUnit mock of the Predis Client matching my expectations?

I'm trying to mock out the Predis client in a PHPUnit test. When I call the method I've tried to mock out, at the end of the test PHPUnit is telling me that the expectation was not met.

Here's a code sample that reproduces my problem:

class MockRedisTest extends \PHPUnit_Framework_TestCase {
private $mockRedis;

public function testMockRedis() {

    $mockRedis =  $this->getMock('Predis\\Client');

    $mockRedis->expects(  $this->once())
      ->method("exists")
      ->with($this->equalTo("query-key"))
      ->will($this->returnValue(true));

    $mockRedis->exists("query-key");
}

}

And PHPUnit thinks the method wasn't called:

1) MockRedisTest::testMockRedis Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.

Why? Is it because the Predis client appears to be using __call to respond to method calls that match redis commands?

UPDATE: I get the impression it has something to do with the __call method. Changing the code to this works:

public function testMockRedis() {

    $mockRedis =  $this->getMock('Predis\\Client');

    $mockRedis->expects(  $this->once())
      ->method("__call")
      ->with("exists", $this->equalTo(array("query-key")))
      ->will($this->returnValue(true));

    $mockRedis->exists("query-key");
}

Not sure I'm satisfied with this though. Is there a better way to mock classes that use __call to proxy methods?


Source: (StackOverflow)

Redis SCARD returning the wrong results?

I'm adding a large number of data points into a redis set:

$t = 0;
$redis = Redis::Connection();
foreach($adv as $a) {
    $t = $t + 1;
    print($t); //Prints to log

    $test = $redis -> sadd('database/ab/AL', $a -> id);
    print($test); //Prints to log
}

When I call redis->scard('database/ab/AL') I get the result 9832, The answer I should be getting is 9866

$t is a counter I put in to check how many iterations the loop is doing, and $t is 9866 after running the loop, which is weird considering scard is returning 9832

Then I thought maybe there are duplicates being added, so I logged the response from sadd

1 [2015-06-29 16:24:55] local.INFO: 1 [] []
2 [2015-06-29 16:24:55] local.INFO: 1 [] []
3 [2015-06-29 16:24:55] local.INFO: 1 [] []
4 [2015-06-29 16:24:55] local.INFO: 1 [] []
5 [2015-06-29 16:24:55] local.INFO: 1 [] []
6 [2015-06-29 16:24:55] local.INFO: 1 [] []
...
9861 [2015-06-29 16:24:59] local.INFO: 1 [] []
9862 [2015-06-29 16:24:59] local.INFO: 1 [] []
9863 [2015-06-29 16:24:59] local.INFO: 1 [] []
9864 [2015-06-29 16:24:59] local.INFO: 1 [] []
9865 [2015-06-29 16:24:59] local.INFO: 1 [] []
9866 [2015-06-29 16:24:59] local.INFO: 1 [] []

There are no zeros in the entire log, which means that each element being added is unique. There are also 9866 log calls which contradicts the scard result. I have tried checking with redis-cli and I still get the wrong results. What gives?


Source: (StackOverflow)

Displaying a large amount of data in paging table without heavily impacting DB

The current implementation is a single complex query with multiple joins and temporary tables, but is putting too much stress on my MySQL and is taking upwards of 30+ seconds to load the table. The data is retrieved by PHP via a JavaScript Ajax call and displayed on a webpage. Here is the tables involved:

Table: table_companies
Columns: company_id, ...

Table: table_manufacture_line
Columns: line_id, line_name, ...

Table: table_product_stereo
Columns: product_id, line_id, company_id, assembly_datetime, serial_number, ...

Table: table_product_television
Columns: product_id, line_id, company_id, assembly_datetime, serial_number, warranty_expiry, ...

A single company can have 100k+ items split between the two product tables. The product tables are unioned and filtered by the line_name, then ordered by assembly_datetime and limited depending on the paging. The datetime value is also reliant on timezone and this is applied as part of the query (another JOIN + temp table). line_name is also one of the returned columns.

I was thinking of splitting the line_name filter out from the product union query. Essentially I'd determine the ids of the lines that correspond to the filter, then do a UNION query with a WHERE condition WHERE line_id IN (<results from previous query>). This would cut out the need for joins and temp tables, and I can apply the line_name to line_id and timezone modification in PHP, but I'm not sure this is the best way to go about things.

I have also looked at potentially using Redis, but the large number of individual products is leading to a similarly long wait time when pushing all of the data to Redis via PHP (20-30 seconds), even if it is just pulled in directly from the product tables.

  • Is it possible to tweak the existing queries to increase the efficiency?
  • Can I push some of the handling to PHP to decrease the load on the SQL server? What about Redis?
  • Is there a way to architect the tables better?
  • What other solution(s) would you suggest?

I appreciate any input you can provide.

Edit:

Existing query:

SELECT line_name,CONVERT_TZ(datetime,'UTC',timezone) datetime,... FROM (SELECT line_name,datetime,... FROM ((SELECT line_id,assembly_datetime datetime,... FROM table_product_stereos WHERE company_id=# ) UNION (SELECT line_id,assembly_datetime datetime,... FROM table_product_televisions WHERE company_id=# )) AS union_products INNER JOIN table_manufacture_line USING (line_id)) AS products INNER JOIN (SELECT timezone FROM table_companies WHERE company_id=# ) AS tz ORDER BY datetime DESC LIMIT 0,100

Here it is formatted for some readability.

SELECT line_name,CONVERT_TZ(datetime,'UTC',tz.timezone) datetime,... 
  FROM (SELECT line_name,datetime,... 
          FROM (SELECT line_id,assembly_datetime datetime,... 
                    FROM table_product_stereos WHERE company_id=# 

                 UNION 
                SELECT line_id,assembly_datetime datetime,... 
                  FROM table_product_televisions 
                 WHERE company_id=# 
               ) AS union_products 
         INNER JOIN table_manufacture_line USING (line_id)
        ) AS products 
INNER JOIN (SELECT timezone 
            FROM table_companies 
            WHERE company_id=# 
            ) AS tz 
ORDER BY datetime DESC LIMIT 0,100

IDs are indexed; Primary keys are the first key for each column.


Source: (StackOverflow)

ClassNotFoundException in SncRedisExtension Factory" from namespace "Predis\Profile" after composer update

Today after update composer packages I get error message.

ClassNotFoundException in SncRedisExtension.php line 158:
Attempted to load class "Factory" from namespace "Predis\Profile".
Did you forget a "use" statement for "Buzz\Message\Factory\Factory"?

This configuration work some time - now stop without any changes in my side.

here is my Composer.json packages

     "php": ">=5.3.3",
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.2,>=2.2.3,<2.5",
    "doctrine/dbal": "<2.5",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0,>=3.0.12",
    "sensio/framework-extra-bundle": "~3.0,>=3.0.2",

    "stof/doctrine-extensions-bundle": "~1.1@dev",
    "a2lix/translation-form-bundle": "1.*@dev",

    "html2text/html2text": "dev-master",

    "liuggio/statsd-client-bundle": "1.6.*",

    "weotch/phpthumb": "dev-master",

    "egeloen/google-map-bundle": "*",
    "oh/google-map-form-type-bundle": "dev-master",
    "meenie/javascript-packer": "dev-master",
    "natxet/CssMin": "dev-master",
    "apy/datagrid-bundle": "dev-master",

    "webit/sms-common": "dev-master",
    "webit/smsapi": "dev-master",
    "webit/smsapi-bundle": "dev-master",

    "ddeboer/data-import": "dev-master",
    "endroid/qrcode": "1.*@dev",
    "knplabs/knp-snappy-bundle": "^1.2",


    "egeloen/ckeditor-bundle": "2.2.*@dev",
    "knplabs/knp-gaufrette-bundle": "*@dev",
    "vich/uploader-bundle": "0.14.*",
    "friendsofsymfony/jsrouting-bundle": "~1.5",
    "fpn/tag-bundle": "^0.9.0",

    "snc/redis-bundle": "~1.1",

    "instaclick/translation-editor-bundle": "dev-master",
    "white-october/pagerfanta-bundle": "~1.0",
    "friendsofsymfony/user-bundle": "~2.0@dev",

    "payum/payum-bundle": "0.14.*",
    "payum/paypal-express-checkout-nvp": "*@stable",
    "payum/offline": "0.14.*",
    "predis/predis": "0.8.x-dev",
    "symfony/var-dumper": "^2.6",
    "dzasa/open-exchange-rates": "dev-master",
    "h4cc/wkhtmltopdf-amd64": "0.12.2.1"

what can be a reason ? I see this file in github ->

https://github.com/nrk/predis/tree/v1.0/src/Profile

but it is not downloaded on composer update

here is files list

/PhpstormProjects/welasy/vendor/predis/predis/lib/Predis/Profile$ ls -la
razem 116
drwxrwxr-x  2 grek grek  4096 kwi 20 21:53 .
drwxrwxr-x 16 grek grek  4096 kwi 20 21:53 ..
-rw-rw-r--  1 grek grek  1444 kwi 20 21:53 ServerProfileInterface.php
-rw-rw-r--  1 grek grek  5992 kwi 20 21:53 ServerProfile.php
-rw-rw-r--  1 grek grek  6521 kwi 20 21:53 ServerVersion12.php
-rw-rw-r--  1 grek grek  9342 kwi 20 21:53 ServerVersion20.php
-rw-rw-r--  1 grek grek 10846 kwi 20 21:53 ServerVersion22.php
-rw-rw-r--  1 grek grek 11033 kwi 20 21:53 ServerVersion24.php
-rw-rw-r--  1 grek grek 12410 kwi 20 21:53 ServerVersion26.php
-rw-rw-r--  1 grek grek 13595 kwi 20 21:53 ServerVersion28.php
-rw-rw-r--  1 grek grek 13659 kwi 20 21:53 ServerVersion30.php
-rw-rw-r--  1 grek grek   711 kwi 20 21:53 ServerVersionNext.php

Source: (StackOverflow)

redis timeout with predis

I'm using redis with php (predis at http://github.com/nrk/predis/) and am experiencing frequent timeout. The stack trace shows:

[04-Apr-2010 03:39:50] PHP Fatal error:  Uncaught exception 'Predis_ClientException' with message 'Connection timed out' in redis.php:697
Stack trace:
#0 redis.php(757): Predis_Connection->connect()
#1 redis.php(729): Predis_Connection->getSocket()
#2 redis.php(825): Predis_Connection->writeCommand(Object(Predis_Commands_ListRange))
#3 redis.php(165): Predis_ConnectionCluster->writeCommand(Object(Predis_Commands_ListRange))
#4 redis.php(173): Predis_Client->executeCommandInternal(Object(Predis_ConnectionCluster), Object(Predis_Commands_ListRange))
#5 redis.php(157): Predis_Client->executeCommand(Object(Predis_Commands_ListRange))
#6 [internal function]: Predis_Client->__call('lrange', Array)

This happens consistently and I have no idea why. Anyone has any idea?


Source: (StackOverflow)

Predis timeout with high traffic site

Im running a website with high traffic peaks (around 10k online users) and Im facing this error with Predis when the site exceeds around 7k online users:

PHP Fatal error:  Uncaught exception 'Predis\\Connection\\ConnectionException' with message 'Connection timed out [tcp://127.0.0.1:6379]'

The server is Intel Xeon 3.4GHz, 32GB RAM and SSD disks, with nginx for static content, apache and mysql. Memory consumption never exceeds 9GB and CPU load is 4 in the peaks.

Im not seeing PHP ouf of memory errors, nor MySQL gone away but only this Redis error. I put timeout 0 and ?read_write_timeout=0 with no luck. Can you help me?

Thanks and sorry for my english!


Source: (StackOverflow)

predis: ZADD with NX keeps throwing error 'Predis\ServerException'

I am having an issue when I try to add "NX" to the zadd command on predis. The redis docs say that ZADD should support NX, but no matter how I set up the predis command, I can not get it working. Does anyone have any experience with this issue?

Here are the commands I have tried:

$redis->zadd($key, "NX", 1, $id);

$redis->executeRaw([ 'ZADD', $key, "NX", 1, $id ]);

Here is the error that keeps getting thrown:

ERROR: exception 'Predis\ServerException' with message 'ERR syntax error'

Looking at the redis-cli monitor, I see the command execute when using the ZADD command, but the executeRaw command does nothing.

Any help would be greatly appreciated!


Source: (StackOverflow)

Predis persistant with PhpiredisStreamConnection

I am working on a Webservice and I'm using Predis as a redis library and I want the clients to be able to reuse the same TCP socket.

Currently , after running a load test on my service , I found that the number of socket in TIME_WAIT state increase fast on the server and at some point the connexion to Redis server gets refused.

According Redis documentation using PhpiredisStreamConnection with presistant option fixes the problem , but after adding this to my connect code , I m still facing the same issue. Any ideas?

Im using TCP connection as the redis instance are not on front servers.

<?php

include 'autoload.php';

$parameters = array(
'tcp://some.host01:6379?database=0&alias=master&persistent=1',
'tcp://some.host02:6379?database=0&alias=slave&persistent=1',
);

$options = array(   'replication' => true ,
'connections' =>  array('tcp'  => 'Predis\Connection\PhpiredisStreamConnection','unix' => 'Predis\Connection\PhpiredisStreamConnection') );

$predis = new Predis\Client($parameters, $options);

?>

Source: (StackOverflow)

Redis scan skipping keys

I'm using predis (with laravel if it makes any difference) php client to work with Redis.

I need to fetch all the keys from Redis that match certain prefix and I do it like this:

$keys = [];
    foreach (new Iterator\Keyspace($this->redis(), Cache::KEY_PREFIX.'*') as $key) {
        $keys[] = $rate_key;
    }

After the work with those keys is done, operation repeats - I'm getting those keys again in again in a loop. I noticed that after a few iterations some keys are not included in $keys array.

The strangest thing is that keys that disappear never appear in next iterations. Restart of the php process (it's a daemon) fixes the problem.

I'm using Redis 3.0.2 with Predis 1.0 and PHP 5.4

P.S. Within the loop over keys, I change values for some of them. I'm not deleting any keys, however.


Source: (StackOverflow)