linux下redis安装:

 

 redis官网地址:http://www.redis.io/

     最新版本:3.0.0

     在Linux下安装Redis非常简单,具体步骤如下(官网有说明):

     1、下载源码,解压缩后编译源码。

$ wget http://download.redis.io/releases/redis-3.0.0.tar.gz
$ tar xzf redis-3.0.0.tar.gz
$ cd redis-3.0.0
$ make
$ make test
$ make install

   安装完毕后,

  3、启动Redis服务。

$ redis-server   redis.conf

     4、然后用客户端测试一下是否启动成功。

$ redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

 

 

 

 

 

 

 

 

 

java使用API:

 

 

package com.reapal.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
* Created by dell on 2015/1/21.
*/
public class RedisAPI {
private static JedisPool pool = null;

/**
* 构建redis连接池
*
* @param
* @param
* @return JedisPool
*/
public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxActive(500);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(5);
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWait(1000 * 100);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
//tanfei 正式环境
// pool = new JedisPool(config, "10.1.11.77", 6379);
pool = new JedisPool(config, "192.168.0.28", 6379);
}
return pool;
}

/**
* 返还到连接池
*
* @param pool
* @param redis
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}


/**
* 获取数据
*
* @param key
* @return
*/
public static String set(String key,String value){

String setreturn = null;

JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.set(key,value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return setreturn;

}

/**
* 获取数据
*
* @param key
* @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;
}

public static void main(String[] arg0){
JedisPool jp = RedisAPI.getPool();
Jedis jedis = jp.getResource();
jedis.set("1","jackchen") ;
System.out.println("-------------------------------");
System.out.println( jedis.get("1"));
}

}

posted on 2015-04-17 10:22  jack-cooper  阅读(364)  评论(0编辑  收藏  举报