程序语言(python、php、Node.js)调用Redis

版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://blog.csdn.net/wh211212/article/details/53037946

Redis:在Python上使用

  • [1]安装Python Redis客户端库
 [root@vdevops ~]# yum --enablerepo=epel -y install python-redis 
  • Python的基本用法
[wang@vdevops ~]$ vim use_redis.py 
#!/usr/bin/env python

import redis

client = redis.StrictRedis(host='127.0.0.1', port=6379, db=0, password='password')

# set and get Key
client.set("key01", "value01")
print "key01.value :", client.get("key01")

# append and get Key
client.append("key01", ",value02")
print "key01.value :", client.get("key01")

client.set("key02", 1)

# increment
client.incr("key02", 100)
print "key02.value :", client.get("key02")

# decrement
client.decr("key02", 51)
print "key02.value :", client.get("key02")

# list
client.lpush("list01", "value01", "value02", "value03")
print "list01.value :", client.lrange("list01", "0", "2")

# hash
client.hmset("hash01", {"key01": "value01", "key02": "value02", "key03": "value03"})
print "hash01.value :", client.hmget("hash01", ["key01", "key02", "key03"])

# set
client.sadd("set01", "member01", "member02", "member03")
print "set01.value :", client.smembers("set01")
[wang@vdevops ~]$ python use_redis.py 
key01.value : value01
key01.value : value01,value02
key02.value : 101
key02.value : 50
list01.value : ['value03', 'value02', 'value01']
hash01.value : ['value01', 'value02', 'value03']
set01.value : set(['member02', 'member03', 'member01'])

Redis:在PHP上使用

  • [1]安装PHP Redis客户端模块
[root@vdevops ~]# yum --enablerepo=epel -y install php-pecl-redis
  • PHP的基本用法
 [wang@vdevops ~]$ vim use_redis.php

<?php
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
$redis->auth("password");

// set and get Key
$redis->set('key01', 'value01');
print 'key01.value : ' . $redis->get('key01') . "\n";

// append and get Key
$redis->append('key01', ',value02');
print 'key01.value : ' . $redis->get('key01') . "\n";

$redis->set('key02', 1);
print 'key02.value : ' . $redis->get('key02') . "\n";

// increment
$redis->incr('key02', 100);
print 'key02.value : ' . $redis->get('key02') . "\n";

// decrement
$redis->decr('key02', 51);
print 'key02.value : ' . $redis->get('key02') . "\n";

// list
$redis->lPush('list01', 'value01');
$redis->rPush('list01', 'value02');
print 'list01.value : ';
print_r ($redis->lRange('list01', 0, -1));

// hash
$redis->hSet('hash01', 'key01', 'value01');
$redis->hSet('hash01', 'key02', 'value02');
print 'hash01.value : ';
print_r ($redis->hGetAll('hash01'));

// set
$redis->sAdd('set01', 'member01');
$redis->sAdd('set01', 'member02');
print 'set01.value : ';
print_r ($redis->sMembers('set01'));
?>

# run

[wang@vdevops ~]$ php use_redis.php

key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
list01.value : Array
(
    [0] => value01
    [1] => value02
)
hash01.value : Array
(
    [key01] => value01
    [key02] => value02
)
set01.value : Array
(
    [0] => member01
    [1] => member02
)

Redis:在Node.js上使用

  • 安装Redis客户端模块
[wang@vdevops ~]$ npm install redis 
# 安装Node.js传送门:
  • Node.js的基本用法
 [wang@vdevops ~]$ vi use_redis.js

var redis = require('redis');
var client = new redis.createClient();

client.auth('password');

// set and get Key
client.set('key01', 'value01');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

// append and get Key
client.append('key01', ',value02');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

client.set('key02', 1);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// increment
client.incrby('key02', 100);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// decrement
client.decrby('key02', 51);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// list
client.rpush('list01', 'value01');
client.rpush('list01', 'value02');
client.lrange('list01', 0, -1, function (err, val) {
    console.log("list01.value :", val);
});

// hash
client.hset("hash01", "key01", "value01");
client.hset("hash01", "key02", "value02");
client.hgetall('hash01', function (err, val) {
    console.log("hash01.value :", val);
});

// set
client.sadd("set01", "member01");
client.sadd("set01", "member02");
client.smembers('set01', function (err, val) {
    console.log("set01.value :", val);
});

# run

[wang@vdevops ~]$ node use_redis.js

key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
list01.value : [ 'value01', 'value02' ]
hash01.value : { key01: 'value01', key02: 'value02' }
set01.value : [ 'member01', 'member02' ]

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券