{"id":19675823,"url":"https://github.com/progerxp/predis-doc","last_synced_at":"2025-08-24T00:13:05.778Z","repository":{"id":146023855,"uuid":"66096629","full_name":"ProgerXP/predis-doc","owner":"ProgerXP","description":"Predis v1 documentation","archived":false,"fork":false,"pushed_at":"2016-08-19T16:43:37.000Z","size":32,"stargazers_count":14,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-10T04:25:30.385Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ProgerXP.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-19T16:31:56.000Z","updated_at":"2024-03-16T20:52:14.000Z","dependencies_parsed_at":"2023-04-11T16:46:49.905Z","dependency_job_id":null,"html_url":"https://github.com/ProgerXP/predis-doc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProgerXP%2Fpredis-doc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProgerXP%2Fpredis-doc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProgerXP%2Fpredis-doc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProgerXP%2Fpredis-doc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProgerXP","download_url":"https://codeload.github.com/ProgerXP/predis-doc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240985483,"owners_count":19889101,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T17:26:01.246Z","updated_at":"2025-02-27T05:41:43.554Z","avatar_url":"https://github.com/ProgerXP.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"Formatted version of this page is available online: http://squizzle.me/php/predis/doc/\n\n# Predis - a PHP interface to Redis\n\nThis documentation is based on [Predis](https://github.com/nrk/predis) 1.1.2-dev.\n\nSource Markdown files are available [on GitHub](https://github.com/ProgerXP/predis-doc) or directly at [this path](md/Commands.md).\n\nPlease submit corrections [via GitHub](https://github.com/ProgerXP/predis-doc/issues).\n\n## Documentation map\n\n* [Classes](Classes.md) - in-depth reference to Predis classes\n* [Commands](Commands.md) - Redis command reference and their Predis implementation details\n* [Configuration](Configuration.md) - server connection and Predis options\n\n### Calling Redis commands\n\n```PHP\n$oldValue = $predis-\u003egetset('skey', 'newvalue');\nvar_dump($oldValue);    //=\u003e string(3) \"foo\"\n```\n\n[Command reference](Commands.md): supported Redis commands (`GETSET`, `SORT`, `HGET`, etc.) and their arguments.\n\n[The `Client` class API](Classes.md#client).\n\n### Connection configuration\n\n```PHP\n$connections = [\n  ['database' =\u003e 3, 'alias' =\u003e 'primary'],\n  ['host' =\u003e 'backupredis.example.com', 'scheme' =\u003e 'tls'],\n];\n\n$predis = new Predis\\Client($connections);\n```\n\n[Using a connection string](Classes.md#parse):\n```PHP\n$predis = new Predis\\Client('unix:///var/run/redis.sock?database=5');\n```\n\n[The **connections** option](Configuration.md#connections): backends (`PhpiredisStreamConnection`) and their schemes (`tcp`, `unix`, etc.).\n\n### Predis options\n\n```PHP\n$options = ['exceptions' =\u003e false];\n$predis = new Predis\\Client([], $options);\n```\n\n[Configuration reference](Configuration.md): supported Predis options for `Client` constructor.\n\n### Default connection parameters\n\n```PHP\n$connections = [\n  ['alias' =\u003e 'primary'],\n  ['port' =\u003e 6380, 'alias' =\u003e 'backup'],\n];\n\n$options = [['parameters' =\u003e ['host' =\u003e 'redis.example.com', 'scheme' =\u003e 'tls']];\n\n$predis = new Predis\\Client($connections, $options);\n```\n\nThe above is equivalent to:\n```PHP\n$connections = [\n  [\n    'host' =\u003e 'redis.example.com',\n    'scheme' =\u003e 'tls',\n    'alias' =\u003e 'primary',\n  ],\n  [\n    'host' =\u003e 'redis.example.com',\n    'scheme' =\u003e 'tls',\n    'port' =\u003e 6380,\n    'alias' =\u003e 'backup',\n  ],\n];\n\n$predis = new Predis\\Client($connections);\n```\n\n[The **parameters** option](Configuration.md#parameters).\n\n### Pipelining\n\n[Basic](Classes.md#pipeline):\n```PHP\n$res = $predis-\u003epipeline(function ($p) {\n  $p-\u003eset('skey', 'v');\n  $p-\u003ehmset('hkey', ['k' =\u003e 'v', 'k2' =\u003e 'v2']);\n  $p-\u003eget('skey');\n  $p-\u003eget('skey2');\n});\n\n// $res has 4 members - one per each command ran.\n```\n\nCombined with a transaction ([options](Classes.md#pipeline)):\n```PHP\n$res = $predis-\u003epipeline(['atomic' =\u003e true], function ($p) {\n  ...\n});\n```\n\n[Custom transaction nesting](Classes.md#nested):\n```PHP\n$res = $predis-\u003epipeline(function ($p) {\n  $p-\u003eget('notintrans');\n\n  $p-\u003emulti();\n  $p-\u003eset('skey', 'intrans');\n  $p-\u003eexec();\n\n  $p-\u003eget('againnotintrans');\n});\n\n// $res has 5 members, with array 4th (EXEC) containng 1 member.\n```\n\nUsing fluent interface:\n```PHP\n$res = $predis-\u003epipeline(['atomic' =\u003e true])\n  -\u003eget('skey')\n  -\u003eset('skey', 'value')\n  -\u003eexecute();\n```\n\n### Transactions\n\n[Basic](Classes.md#transaction):\n```PHP\n$res = $predis-\u003etransaction(function ($t) {\n  $t-\u003eget('skey');\n  $t-\u003ehset('hkey', 'k', 'v');\n});\n\n// $res has 2 members.\n```\n\nFaining on key(s) changes (`WATCH`) with up to two retries:\n```PHP\ntry {\n  $res = $predis-\u003etransaction(['watch' =\u003e 'wkey', 'retry' =\u003e 2], function ($t) {\n    ...\n  });\n} catch (Transaction\\AbortedMultiExecException $e) {\n  die(\"Still couldn't save the changes after 3 attempts.\");\n}\n```\n\n[Compare-And-Swap](Classes.md#cas):\n```PHP\n$res = $predis-\u003etransaction(['watch' =\u003e 'wkey', 'cas' =\u003e true], function ($t) {\n  $value = $t-\u003eget('wkey');\n  $t-\u003emulti();\n  $t-\u003eset('wkey', $value.'foo');\n});\n```\n\nUsing fluent interface:\n```PHP\n$res = $predis-\u003etransaction(['exceptions' =\u003e false])\n  -\u003eset('k1', 'v1')\n  -\u003eset('k2', 'v2')\n  -\u003eexecute();\n\n// With 'exceptions' unset, failed commands will return an ErrorInterface object instead of throwing ServerException.\n```\n\n### Pub/Sub subscription\n\n[Basic](Classes.md#pubsubloop):\n```PHP\n$predis-\u003epubSubLoop(['subscribe' =\u003e 'chan'], function ($l, $msg) {\n  if ($msg-\u003epayload === 'Q') {\n    return false;\n  } else {\n    echo \"$msg-\u003epayload on $msg-\u003echannel\", PHP_EOL;\n  }\n});\n```\nUsing a [consumer object](Classes.md#abstractconsumer):\n```PHP\n$l = $predis-\u003epubSubLoop(['subscribe' =\u003e 'chan']);\n\nforeach ($l as $msg) {\n  if ($msg-\u003epayload === 'unsub') {\n    $l-\u003eunsubscribe('chan');\n  } elseif ($msg-\u003epayload === 'psub') {\n    $l-\u003epsubscribe('chan:*');\n  } elseif ($msg-\u003epayload === 'Q') {\n    $l-\u003estop();\n  }\n}\n\necho 'pub/sub has stopped.';\n```\n\nUsing [per-channel callbacks](Classes.md#dispatcherloop):\n```PHP\n$l = predis-\u003epubSubLoop();\n$dl = new Predis\\PubSub\\DispatcherLoop($l);\n\n$dl-\u003eattachCallback('chan1', function ($payload) {\n  echo \"Got $payload on chan1.\", PHP_EOL;\n});\n\n$dl-\u003eattachCallback('ctlchan', function ($payload) use ($dl) {\n  echo \"Received a message on ctlchan, stopping.\";\n  $dl-\u003estop();\n});\n\n$dl-\u003edefaultCallback(function ($msg) {\n  echo \"Received a message on $msg-\u003echannel.\", PHP_EOL;\n});\n\n$l-\u003epsubscribe('foo:*');\n\n$l-\u003erun();\n```\n\n### Iterating over keys and members\n\n[All keys in a database](Classes.md#keyspace) (`SCAN`):\n```PHP\n$it = new Predis\\Collection\\Iterator\\Keyspace($predis);\n\nforeach ($it as $key) {\n  echo \"Found a key named '$key'\", PHP_EOL;\n}\n```\n\n[Hash fields](Classes.md#hashkey) (`HSCAN`):\n```PHP\n$it = new Predis\\Collection\\Iterator\\HashKey($predis, 'hkey');\n\nforeach ($it as $key =\u003e $value) {\n  echo \"Found a field '$key', value '$value' in hash key 'hkey'\", PHP_EOL;\n}\n```\n\n[Set members](Classes.md#setkey) (`SSCAN`):\n```PHP\n$it = new Predis\\Collection\\Iterator\\SetKey($predis, 'setkey');\n\nforeach ($it as $member) {\n  echo \"Found a member '$member' in set key 'setkey'\", PHP_EOL;\n}\n```\n\n[Sorted set members](Classes.md#sortedsetkey) (`ZSCAN`):\n```PHP\n$it = new Predis\\Collection\\Iterator\\SortedSetKey($predis, 'zkey');\n\nforeach ($it as $member =\u003e $score) {\n  echo \"Found a member '$member', score '$score' in sorted set key 'zkey'\", PHP_EOL;\n}\n```\n\n[List values](Classes.md#listkey) (emulation with `LRANGE`):\n```PHP\n$it = new Predis\\Collection\\Iterator\\ListKey($predis, 'lkey');\n\nforeach ($it as $member) {\n  echo \"Found a member '$member' in list key 'lkey'\", PHP_EOL;\n}\n```\n\n### Registering Lua scripts\n\n```PHP\nclass EchoScript extends Predis\\Command\\ScriptCommand {\n  function getScript() {\n    return 'return ARGV[1]';\n  }\n}\n\n$predis-\u003egetProfile()-\u003edefineCommand('echolua', 'EchoScript');\n\necho $predis-\u003eecholua('foo');\n  //=\u003e foo\n```\n\n[The `ScriptCommand` Class](Classes.md#scriptcommand).\n\n### Session handler\n\n```PHP\n$h = new Session\\Handler($predis);\n$h-\u003eregister();\nsession_start();\n```\n\n[The `Session\\Handler` class](Classes.md#session): using Redis for storing session data.\n\n### Other topics\n\n* [List of commands](Configuration.md#supported) supported by target Redis server (Predis \"profile\")\n* [List of Predis exceptions](Classes.md#list)\n* [List of Predis interfaces](Classes.md#list-of)\n* Details about default [StreamConnection](Classes.md#streamconnection) used for handling `tcp`, `tls`, `unix` and `redis`/`rediss` schemes,\n* ...about [WebdisConnection](Classes.md#webdisconnection) handling `http` scheme, about `phpiredis` using [PHP streams](Classes.md#phpiredisstreamconnection) and [sockets extension](Classes.md#phpiredissocketconnection)\n\n\n## Syntax conventions\n\nFunction definitions include arguments type hinting similar to those used in the PHP manual. In cases no type hint is present, a string type - `str` - is implied. Usually if an invalid type is given there will be a PHP error or implicit coersion so don't do that.\n\nSometimes argument `$name` is omitted and only its type hint is left for clarity. This is often done with object arguments: `SomeInterface $some` is written as just `SomeInterface`.\n\nThe `$key` argument always contains name of a Redis key entry such as `foo:123`. `$src` and `$dest` arguments are similar but additionally indicate that the operation will take data from (`$src`) or put data into (`$dest`) them.\n\nIf arguments are omitted with an ellipsis: `brpop(...)` - they are entirely dictated by another source indicated by the `See` reference in the description.\n\nMany functions are variadic, i.e. accepting arbitrary number of arguments. Some functions even have several \"variadic\" argument groups. Variability is indicated by the `[...]` pattern (where square brackets mean optional data as per standard BNF notation). For example:\n```PHP\neval($script, $numKeys[, $key[...]][, $arg[...]]);\n```\n\nThe definition above contains 2 variadic groups: `$key` and `$arg`. Examples of proper invocation:\n```PHP\neval('s', 1, 'k', 'v', 'v2');\neval('s', 1, 'k');\neval('s', 0, 'v', 'v2');\neval('s', 0);\n```\n\nExamples in the Commands section omit object reference from method invokaction for clarity. Thus, the above example in real code looks like this:\n```PHP\n$predis = new Predis;\n$predis-\u003eeval('s', 1, 'k', 'v', 'v2');\n...\n```\n\nAll examples assume their base namespace as `Predis` so that `Command\\HashGet` refers to `Predis\\Command\\HashGet`. In some cases class names are further reduced since most are unique Predis-wise. Thus `ConnectionException` refers to `Predis\\Connection\\ConnectionException`.\n\nThe `$predis` variable in the examples refers to a previously set up `Predis\\Client` object.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogerxp%2Fpredis-doc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogerxp%2Fpredis-doc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogerxp%2Fpredis-doc/lists"}