一、简介
1、redis是一个开源的、高性能的、基于键值对的缓存和存储系统,通过提供多种键值数据类型适应不同场景下的缓存和存储需求,同时redis高级功能能胜任消息队列、任务队列等不同角色.
2、内存存储与持久化:redis中所有数据都存储在内存中.但有问题,程序退出的时候内存中的数据会丢失,不过redis提供对持久化的支持,即将内存中的数据异步写入到硬盘中,不影响提供服务.
3、redis可以为每个键设置生存时间,到期后会自动删除,这一功能让redis成为了出色的缓存系统.作为缓存系统,redis还可以限定占用的最大内存空间,在数据达到空间限制后可以按照一定规则自动淘汰不需要的键.
二、安装
1、将 redis 安装到/usr/local/webserver/redis
mkdir -p /usr/local/webserver/redis
2、下载安装包
wget http://download.redis.io/redis-stable.tar.gz tar xzf redis-stable.tar.gz cd redis-stable make //报错的话 install gcc
make install //redis可执行文件redis-cli,redis-server等会被复制到/usr/local/bin,命令行直接输入即可执行
最常用的两个程序是:redis-server是redis服务器,启动redis即运行redis-server;redis-cli是reids自带的命令行客户端.
3、启动redis
1>直接运行:$ redis-server
2>通过初始化脚本,这样能在生产环境中,使redis随系统自动运行
(1)配置初始化脚本,将redis源码目录中的utils文件夹中的redis_init_script复制到/etc/init.d,命名为redis_6379,内容不做改动:
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac