redis的简单使用和介绍 linux(centos 5.4) redis install

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP,Java客户端,使用很方便。
Redis使用单线程的IO复用模型,自己封装了一个简单的AeEvent事件处理框架,主要实现了epoll、kqueue和select,对于单纯只有IO操作来说,单线程可以将速度优势发挥到最大,但是Redis也提供了一些简单的计算功能,比如排序、聚合等,对于这些操作,单线程模型实际会严重影响整体吞吐量,CPU计算过程中,整个IO调度都是被阻塞住的。

Redis除了作为存储之外还提供了一些其它方面的功能,比如聚合计算、pubsub、scripting等,对于此类功能需要了解其实现原理,清楚地了解到它的局限性后,才能正确的使用,比如pubsub功能,这个实际是没有任何持久化支持的,消费方连接闪断或重连之间过来的消息是会全部丢失的,又比如聚合计算和scripting等功能受Redis单线程模型所限,是不可能达到很高的吞吐量的,需要谨慎使用。

本例子Linux采用的centOs5.4

下面来介绍一下redis的安装

wget  http://redis.googlecode.com/files/redis-2.0.4.tar.gz
tar zxvf redis-2.0.4.tar.gz
cd  redis-2.0.4
make

make完后 redis-2.0.4目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli

安装成功

启动服务

./redis-server

也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动

./redis-server redis.conf

redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了

注意启动的时候,会出现

WARNING overcommit_memory is set to 0!Background save may fail under 

low memory condition. To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and 

[6020] 10 Aug 20:58:21 * The server is nowready to accept connections on port 6379

[6020] 10 Aug 20:58:21 - 0 clientsconnected (0 slaves), 533432 bytes in use

[6020] 10 Aug 20:58:30 - 0 clientsconnected (0 slaves), 533432 bytes in use

还有就是执行:sysctl vm.overcommit_memory=1

关于redis一些资料的学习可以到http://www.cnblogs.com/xhan/archive/2011/02/08/1949867.html去学习 ,很全面

下面介绍一个简单java客户端Jedis,大家可以到https://github.com/xetorthio/jedis这网址下载

这里给大家提供一个简单的对jedis的封装类以供参考

Redis.java

package com.ajun.redis;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author ajun
 *
 */
public class Redis {
	private static JedisPool pool;
	private static int DBIndex=1;
	private static String host="192.168.1.200";
	private static int port=6379;
	private static int timeout=60*1000;
	static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(100);
		config.setMaxIdle(20);
		config.setMaxWait((long)1000);		
		config.setTestOnBorrow(false);			
		pool = new JedisPool(config, host, port, timeout);//线程数量限制,IP地址,端口,超时时间			
		
	}
	
	
	/**
	 *  注意:
	 *  作为一个key value存在,很多开发者自然的使用set/get方式来使用Redis,实际上这并不是最优化的使用方法。
	 *  尤其在未启用VM情况下,Redis全部数据需要放入内存,节约内存尤其重要。
		假如一个key-value单元需要最小占用512字节,即使只存一个字节也占了512字节。
		这时候就有一个设计模式,可以把key复用,几个key-value放入一个key中,value再作为一个set存入,
		这样同样512字节就会存放10-100倍的容量。
		用于存储多个key-value的值,比如可以存储好多的person Object
		例子:>redis-cli
		存储:redis 127.0.0.1:6379> hset personhash personId personObject
		获得:redis 127.0.0.1:6379> hget personhash personId (就可以获得当前personId对应的person对象)
	 * @param key hashset key
	 * @param field 相当于personId
	 * @param value  person Object
	 */
	public static void hsetItem(String key,String field,byte [] value){
		Jedis jedis=null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			jedis.hset(key.getBytes(), field.getBytes(), value);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
	}
	
	public static byte []  hgetItem(String key,String field){
		Jedis jedis=null;
		byte [] value = null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			value= jedis.hget(key.getBytes(), field.getBytes());
			//jedis.hgetAll(key);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
		return value;
	}
	
	/**
	 * @param key
	 * @param value
	 * @param seconds 有效时间 秒为单位 0为永久有效
	 */
	public static void setItem(String key ,byte [] value,int seconds){
		Jedis jedis=null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			if(seconds==0){
				jedis.set(key.getBytes(), value);
			}else{
				jedis.setex(key.getBytes(), seconds, value);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
	}
	
	
	
	
	/**
	 * 删除
	 * @param keys
	 */
	public static void del(String... keys){
		Jedis jedis=null;
		if(keys!=null){
			try {
				jedis = pool.getResource();
				jedis.connect();
				jedis.select(DBIndex);
				jedis.del(keys);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(jedis!=null)
					pool.returnResource(jedis);
			}
		}
	}
	

	
	/**
	 * 头部添加元素
	 * @param key
	 * @param value
	 */
	public static void lpushToList(String key,byte[] value){
		Jedis jedis=null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			jedis.lpush(key.getBytes(), value);	
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
		}
	}
	
	/**
	 * 返回List
	 * @param key
	 * @param value
	 */
	public static List<byte[]>  lrangeFromList(String key,int start ,int end){
		Jedis jedis=null;
		List<byte[]> list = null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			list = jedis.lrange(key.getBytes(), start, end);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
		}
		return list;
	}
	
	/**
	 * 
	 * @param key key
	 * @param member 存储的value
	 * @param score 排序字段 一般为objecId
	 */
	public static void addItemToSortSet(String key,byte[] member,double score){
		Jedis jedis=null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			jedis.zadd(key.getBytes(), score, member);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
	}
	
	public static void addListToSortSet(String key,List<byte[]> list,List<Double> scores){
		Jedis jedis=null;
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			if(list!=null&& !list.isEmpty()&& scores!=null&& !scores.isEmpty() && list.size()==scores.size()){
				for(int i=0;i<list.size();i++){
					jedis.zadd(key.getBytes(), scores.get(i), list.get(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
	}
	
	public static List<byte[]> getFromSortSet(String key,int start ,int end,OrderStatus orderStatus){
		Jedis jedis=null;
		List<byte[]> list = new ArrayList<byte[]>();
		Set<byte[]> set= new TreeSet<byte[]>();
		try {
			jedis = pool.getResource();
			jedis.connect();
			jedis.select(DBIndex);
			if(orderStatus.equals(OrderStatus.DESC)){
				set = jedis.zrevrange(key.getBytes(), start, end);
			}else{
				set = jedis.zrange(key.getBytes(), start, end);
			}
			if(set!=null && !set.isEmpty()){
				for(byte[] b:set){
					list.add(b);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis!=null)
				pool.returnResource(jedis);
		}
		return list;
	}

	public static byte[] getItem(String key)
	{
		Jedis jedis=null;
		byte[] s=null;
		try {
			jedis = pool.getResource();
			jedis.select(DBIndex);
			s = jedis.get(key.getBytes());
			return s;
		} catch (Exception e) {
			e.printStackTrace();
			return s;
		}	
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
			
		}
		
		
	}
	
	public static void delItem(String key)
	{
		Jedis jedis=null;
		try {
			jedis = pool.getResource();			
			jedis.select(DBIndex);			
			jedis.del(key.getBytes());
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
		}
		
	}
	public static long getIncrement(String key)
	{
		Jedis jedis=null;
		try {
			jedis = pool.getResource();			
			jedis.select(DBIndex);			
			return jedis.incr(key);
		
		} catch (Exception e) {
			e.printStackTrace();
			return 0L;
		}
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
		}
		
	}
	
	public static void getkeys(String pattern){
		
		Jedis jedis=null;
		try {
			jedis = pool.getResource();			
			jedis.select(DBIndex);	
			Set<String> keys = jedis.keys(pattern);
			for(String b:keys){
				System.out.println("keys==> "+b);
				
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(jedis!=null)
			pool.returnResource(jedis);
		}
		
	}

}


你可能感兴趣的:(redis,exception,linux,centos,String,scripting)