Fork me on GitHub

redis安装、使用

官网:http://redis.io/

github地址:https://github.com/antirez/redis

简介:

        redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
        Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。
 
redis工具类实现:

maven依赖
<!-- jedis:redis clients -->
<dependency>
       <groupId>commons-pool</groupId>
       <artifactId>commons-pool</artifactId>
       <version>1.6</version>
</dependency>
<dependency>
       <groupId>redis.clients</groupId>
       <artifactId>jedis</artifactId>
       <version>2.7.2</version>
       <type>jar</type>
       <scope>compile</scope>
</dependency>
 
jedis客户端工具类(Simple+Pool+ShardedPool三种方式)+对比XMemcached客户端工具类【分片ShardedPool、主从】
..jedis客户端工具类(Simple+Pool+ShardedPool三种方式)+对比XMemcached客户端工具类.rar..
 
虽然,redis支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作;
但是,上述集合操作抽象到工具类中太啰嗦了;
因此,改版本工具类中,只针对Object和String类型封装了工具方法;
如需操作集合,可查看教程:http://javacrazyer.iteye.com/blog/1840161    

windows安装redis:

Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本。
解压后的bin目录下有以下这些文件:
    redis-benchmark.exe         #基准测试
    redis-check-aof.exe         # aof
    redis-check-dump.exe        # dump
    redis-cli.exe               # 客户端
    redis-server.exe            # 服务器
    redis.windows.conf          # 配置文件
 
配置:修改配置文件redis.windows.conf
.......启动内存.......
# maxheap <bytes>
maxheap 1024000000
.......
Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
 
注册/注销系统服务,启动/停止
批处理命令文件【service-install.bat】:redis-server.exe --service-install redis.windows.conf --loglevel verbose    
批处理命令文件【uninstall-service.bat】:redis-server --service-uninstall    
启动关闭命令:net start redis    / net stop redis    
 
非系统服务方式,启动/停止
方式一:双击"redis-server.exe"
方式二:批处理命令文件【startup.bat】:redis-server  redis.windows.conf        
 
测试使用
    a、help查看:> help
    b、help查看String:> help @String
    c、双击redis-cli.exe或者如下
        > telnet 127.0.0.1 6379 
        > set name jack 
        > get name
 
CentOS安装redis:

安装编译工具:
yum install wget  make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel kernel keyutils  patch perl    
安装Redis需要tcl支持:
# yum install -y tcl     
下载安装redis:   
# wget http://download.redis.io/releases/redis-3.0.2.tar.gz    
# tar xvzf redis-3.0.2.tar.gz    
# mv redis-3.0.2 /home/root    
# cd /home/root/redis-3.0.2    
# make    
# make test   
# make install     
 
提示1:如果没有安装gcc,安装命令:yum install gcc-c++    
提示2: You need tcl 8.5 or newer in order to run the Redis test,执行命令:yum install -y tcl    
 
在make成功以后,会在src目录下多出一些可执行文件:redis-server,redis-cli等等
测试通过后安装,安装后会自动把redis-server,redis-cli,redis-benchmark,redis-check-aof,redis-check-dump复制到/usr/local/bin目录下
 
控制台启动(默认端口6379)
配置文件见安装目录下:redis.conf
执行命令:
# cd /home/root/redis-3.0.2/    
# ./src/redis-server     
 
系统服务(守护进程) + 开机启动:
安装的install的时候,redis的命令会被拷贝到/usr/local/bin下面
配置redis.conf拷贝到/usr/local/bin目录下
# cd /home/root/redis-3.0.2/    
# cp redis.conf /usr/local/bin  
# cd /usr/local/bin    
# vi redis.conf     
配置redis.conf文件:修改daemonize no改成yes
编写自init.d脚本:
# vi /etc/init.d/redis    
###########################
#chkconfig: 2345 10 90
#description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
   
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
   
PIDFILE=/var/run/redis.pid
CONF="/usr/local/bin/redis.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
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac
##############################
执行权限
# chmod +x /etc/init.d/redis    
开机自启动
# sudo chkconfig redis on
启动或停止redis
# service redis start  
# service redis stop  

# service redis stop        

开启访问端口6379
# /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT    
# /etc/rc.d/init.d/iptables save    
 
 
posted @ 2016-04-14 21:49  许雪里  阅读(623)  评论(0编辑  收藏  举报