~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to api/lib/Redisent/README.markdown

  • Committer: Keith Hughitt
  • Date: 2012-04-23 16:02:25 UTC
  • mto: This revision was merged to the branch mainline in revision 732.
  • Revision ID: keith.hughitt@nasa.gov-20120423160225-xzoh82ejf37c8yr7
Incorporated HVPull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Redisent
 
2
 
 
3
Redisent is a simple, no-nonsense interface to the [Redis](http://code.google.com/p/redis/) key-value store for modest developers.
 
4
Due to the way it is implemented, it is flexible and tolerant of changes to the Redis protocol.
 
5
 
 
6
## Getting to work
 
7
 
 
8
If you're at all familiar with the Redis protocol and PHP objects, you've already mastered Redisent.
 
9
All Redisent does is map the Redis protocol to a PHP object, abstract away the nitty-gritty, and make the return values PHP compatible.
 
10
 
 
11
    require 'redisent.php';
 
12
    $redis = new Redisent('localhost');
 
13
    $redis->set('awesome', 'absolutely');
 
14
    echo sprintf('Is Redisent awesome? %s.\n', $redis->get('awesome'));
 
15
 
 
16
You use the exact same command names, and the exact same argument order. **How wonderful.** How about a more complex example?
 
17
 
 
18
    require 'redisent.php';
 
19
    $redis = new Redisent('localhost');
 
20
    $redis->rpush('particles', 'proton');
 
21
    $redis->rpush('particles', 'electron');
 
22
    $redis->rpush('particles', 'neutron');
 
23
    $particles = $redis->lrange('particles', 0, -1);
 
24
    $particle_count = $redis->llen('particles');
 
25
    echo "<p>The {$particle_count} particles that make up atoms are:</p>";
 
26
    echo "<ul>";
 
27
    foreach ($particles as $particle) {
 
28
      echo "<li>{$particle}</li>";
 
29
    }
 
30
    echo "</ul>";
 
31
 
 
32
Be aware that Redis error responses will be wrapped in a RedisException class and thrown, so do be sure to use proper coding techniques.
 
33
 
 
34
## Clustering your servers
 
35
 
 
36
Redisent also includes a way for developers to fully utilize the scalability of Redis with multiple servers and [consistent hashing](http://en.wikipedia.org/wiki/Consistent_hashing).
 
37
Using the RedisentCluster class, you can use Redisent the same way, except that keys will be hashed across multiple servers.
 
38
Here is how to set up a cluster:
 
39
 
 
40
    include 'redisent_cluster.php';
 
41
 
 
42
    $cluster = new RedisentCluster(array(
 
43
          array('host' => '127.0.0.1', 'port' => 6379),
 
44
          array('host' => '127.0.0.1', 'port' => 6380)
 
45
    ));
 
46
 
 
47
You can then use Redisent the way you normally would, i.e., `$cluster->set('key', 'value')` or `$cluster->lrange('particles', 0, -1)`.
 
48
But what about when you need to use commands that are server specific and do not operate on keys? You can use routing, with the `RedisentCluster::to` method.
 
49
To use routing, you need to assign a server an alias in the constructor of the Redis cluster. Aliases are not required on all servers, just the ones you want to be able to access directly.
 
50
 
 
51
    include 'redisent_cluster.php';
 
52
 
 
53
    $cluster = new RedisentCluster(array(
 
54
          'alpha' => array('host' => '127.0.0.1', 'port' => 6379),
 
55
          array('host' => '127.0.0.1', 'port' => 6380)
 
56
    ));
 
57
 
 
58
Now there is an alias of the server running on 127.0.0.1:6379 called **alpha**, and can be interacted with like this:
 
59
 
 
60
    // get server info
 
61
    $cluster->to('alpha')->info();
 
62
 
 
63
Now you have complete programatic control over your Redis servers.
 
64
 
 
65
## About
 
66
 
 
67
&copy; 2009 [Justin Poliey](http://justinpoliey.com)
 
 
b'\\ No newline at end of file'