Redis是一个key-value存储系统。和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表)、sets(集合)和zsets(有序集合)几种数据类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
Redis的代码遵循ANSI-C编写,可以在所有POSIX系统(如Linux,*BSD, Mac OS X, Solaris等)上安装运行。而且Redis并不依赖任何非标准库,也没有编译参数必需添加。redis的安装出奇的简单,这可能也是他风靡的一个原因,让人很容易上手
1 下载
下载地址:Redis Download,最新稳定版redis-2.6.9.tar.gz
文档资料: Redis Document
2 安装
$ wget http://redis.googlecode.com/files/redis-2.6.9.tar.gz
$ tar xzf redis-2.6.9.tar.gz
$ cd redis-2.6.9
$ make$ sudo make install
3 命令
1) 修改配置文件
vim /opt/redis-2.8.7/redis.conf
################################ GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
2) 启动服务器
/usr/local/bin/redis-server /opt/redis-2.8.7/redis.conf
2) 开启客户端
src/redis-cli
或
homer@ubuntu:/opt/redis-2.8.7$ /opt/redis-2.8.7/src/redis-cli
127.0.0.1:6379> set foo "bar"
OK
127.0.0.1:6379> get foo
"bar"
连接远程redis:redis-cli -h 172.118.10.114 -p 6399 // 指定 ip 服务器 和 redis 端口号
3) 测试命令
redis 127.0.0.1:6379> set myname "yanggang"
OK
redis 127.0.0.1:6379> get myname
"yanggang"
redis 127.0.0.1:6379> set myblog "http://blog.csdn.net/sunboy_2050"
OK
redis 127.0.0.1:6379> get myblog
"http://blog.csdn.net/sunboy_2050"
帮助命令
127.0.0.1:6379> help redis-cli 2.8.7 Type: "help @<group>" to get a list of commands in <group> "help <command>" for help on <command> "help <tab>" to get a list of possible help topics "quit" to exit 127.0.0.1:6379> help set SET key value [EX seconds] [PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 group: string 127.0.0.1:6379> help get GET key summary: Get the value of a key since: 1.0.0 group: string 127.0.0.1:6379> help expire EXPIRE key seconds summary: Set a key's time to live in seconds since: 1.0.0 group: generic 127.0.0.1:6379> help expireat EXPIREAT key timestamp summary: Set the expiration for a key as a UNIX timestamp since: 1.2.0 group: generic
4 客户端编程
代码:
import redis.clients.jedis.Jedis; public class JedisTest { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo"); System.out.print("foo's value : " + value); } }运行结果:
foo's value : bar
python 连接 Redis
1)前往redis-py下载发布版本release,最新发布版本:redis-py-2.8.0.zip
2)解压redis-py-2.8.0.zip:unzip redis-py-2.8.0.zip, 安装:sudo python setup.py install
3)验证安装成功:
# python
>>> import redis
>>>
参考推荐:
Redis 的安装配置介绍(推荐)