소스 검색

Add an example to show how to leverage key prefixes.

Daniele Alessandri 14 년 전
부모
커밋
20ce6e523a
1개의 변경된 파일31개의 추가작업 그리고 0개의 파일을 삭제
  1. 31 0
      examples/KeyPrefixes.php

+ 31 - 0
examples/KeyPrefixes.php

@@ -0,0 +1,31 @@
+<?php
+require_once 'SharedConfigurations.php';
+
+// Predis ships with a KeyPrefixPreprocessor class that is used to transparently
+// prefix each key before sending commands to Redis, even for complex commands
+// such as SORT, ZUNIONSTORE and ZINTERSTORE. Key prefixes are useful to create
+// user-level namespaces for you keyspace, thus eliminating the need for separate
+// logical databases.
+
+use Predis\Commands\Preprocessors\KeyPrefixPreprocessor;
+
+$client = new Predis\Client();
+$client->getProfile()->setPreprocessor(new KeyPrefixPreprocessor('nrk:'));
+
+$client->mset(array('foo' => 'bar', 'lol' => 'wut'));
+var_dump($client->mget('foo', 'lol'));
+/*
+array(2) {
+  [0]=> string(3) "bar"
+  [1]=> string(3) "wut"
+}
+*/
+
+var_dump($client->keys('*'));
+/*
+array(2) {
+  [0]=> string(7) "nrk:foo"
+  [1]=> string(7) "nrk:lol"
+}
+*/
+?>