Redis 运用

前言java

   本文主要包含如下三个方面的内容:Redis安装及配置、Redis基本操做、小结redis

 

1、Redis安装及配置centos

(1)环境缓存

        OS : CentOS 6.5     官网下载对应版本: http://www.centoscn.com/CentosSoft/iso/bash

        Redis: Redis 3.0.3   官网下载对应版本: http://redis.io/download测试

(2)安装步骤ui

            安装CentOS 6.5 的过程省略。默认为已经有CentOS 6.5 的操做环境,安装Redis 的步骤以下加密

        1> 下载Redis 稳定版本.net

wget http://download.redis.io/redis-stable.tar.gz

        2> 解压命令行

tar xvzf redis-stable.tar.gz

    3> 进入redis-stable 文件夹

cd redis-stable

    4> 开始编译redis

make

   5> 进入到src文件夹

cd src

  6> 安装redis

make install

  7>测试redis

make test

  8>运行时若是出错,大概是tcl版本过低的缘由的话请按照下面的步骤运行,而后再运行 make test 命令。最终会显示运行成功!

wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
sudo tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/
cd  /usr/local/tcl8.6.1/unix/
sudo ./configure
sudo make
sudo make install 

 

      9>安装完毕以后,运行以下命令,就能够启动Redis了

redis-server

  10> redis启动以后,运行客户端命令就能够链接到redis上

redis-cli

  11>链接到redis以后能够运行以下的命令对数据进行增删改查

add  key test       /*  add data  */
get key             /*  get data  */
del key             /*  del data  */

(3)配置说明

         在文件夹redis-stable中找到 redis.conf文件,就能够对redis进行相关配置了,能够根据本身的须要进行相关的配置。

         好比须要添加密码,则在redis.conf文件中 有“#requirepass ”去掉“#” ,而后再关键字requirepass 后添加密码。

       

2、Redis对数据的操做

       对redis的操做能够经过不一样的方式进行,好比直接经过命令行的方式对数据进行操做、经过不一样语言做为客户端对其实现操做数据。本文以Java为例来实现对redis数据的增删改查。使用了第三方库

package com.cx.redis;

import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description :
 *
 * @author : WuBo
 *
 * @date   : 2015年8月4日
 *
 */
public class RedisUtil {
	  
	private static JedisPool pool = null;
	
	/**
	 * @desc 获取链接池
	 * @return
	 */
	public static JedisPool getPool() {
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            //config.setMaxIdle(5);
            config.setMaxTotal(1000);//最大链接数
            //config.setMaxWaitMillis(1000 * 100);
            config.setTestOnBorrow(true); //测试链接是否可用
            pool = new JedisPool(config, "192.168.1.100", 6379,1000 * 100,"xxxx123");
        }
        return pool;
    }
	
	/**
	 * @desc 释放链接
	 * @return
	 */
	 public static void returnResource(JedisPool pool, Jedis redis) {
	        if (redis != null) {
	            pool.returnResourceObject(redis);
	        }
	 }
	 
     
	    /**
		 * @desc 获取值
		 * @return
		 */
	 public static String get(String key){
	        String value = null;
	        JedisPool pool = null;
	        Jedis jedis = null;
	        try {
	            pool = getPool();
	            jedis = pool.getResource();
	            value = jedis.get(key);
	        } catch (Exception e) {
	            //释放redis对象
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返还到链接池
	            returnResource(pool, jedis);
	        }
	        return value;
	    }
	 
	 /**
		 * @desc 随机获取制定set中的元素
		 * @param key
		 * @return
	*/
	 public static String getRandEleOfSet(String key){
			String value = null;
	        JedisPool pool = null;
	        Jedis jedis = null;
	        try {
	            pool = getPool();
	            jedis = pool.getResource();
	            value = jedis.srandmember(key);
	        } catch (Exception e) {
	            //释放redis对象
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返还到链接池
	            returnResource(pool, jedis);
	        }
	        return value;
	 }
	 
	 /**
		 * @desc 随机获取制定set中的全部元素
		 * @param key
		 * @return
	*/
	 public static Set<String> getAllEleOfSet(String key){
		    Set<String> value = null;
	        JedisPool pool = null;
	        Jedis jedis = null;
	        try {
	            pool = getPool();
	            jedis = pool.getResource();
	            value = jedis.smembers(key);
	        } catch (Exception e) {
	            //释放redis对象
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返还到链接池
	            returnResource(pool, jedis);
	        }
	        return value;
	 }
	 
	  //清空缓存数据
	  public static void clearCache(String key){
	    	    JedisPool pool = null;
		        Jedis jedis = null;
		        try {
		            pool = getPool();
		            jedis = pool.getResource();
		            jedis.del(key);
		        } catch (Exception e) {
		            //释放redis对象
		            pool.returnBrokenResource(jedis);
		            e.printStackTrace();
		        } finally {
		            //返还到链接池
		            returnResource(pool, jedis);
		        }
	    }
	  
	  /**
		 * @desc: 添加一个SET集合
		 */
		public static void addElementToSet(String key,String... element){
			JedisPool pool = null;
	        Jedis jedis = null;
	        try {
	            pool = getPool();
	            jedis = pool.getResource();
	            jedis.sadd(key, element);
	        } catch (Exception e) {
	            //释放redis对象
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返还到链接池
	            returnResource(pool, jedis);
	        }
		}
	 
	   /**
		 * @desc 获取redis链接
		 * @return
	   */
	 public static Jedis getJedis(){
		 Jedis jedis = null;
		 try{
			 jedis = getPool().getResource();
		 }catch(Exception e){
			 pool.returnBrokenResource(jedis);
			 e.printStackTrace();
		 }
		 return jedis;
	 }
	 
	   
}

  

3、小结

     本文对Redis的安装、配置、以及相关运用作了一个简单总结做为本身的一个记录吧。

相关文章
相关标签/搜索