Redis服務的安裝使用

1、下載源碼,解壓縮後編譯源碼。

# tar -zxzf redis-2.8.3.tar.gz

# cd redis-2.8.3

# make && make install

2、編譯完成後

有四個可執行文件redis-server、redis-benchmark、redis-cli和redis.conf。然後拷貝到一個目錄下。

# mkdir /usr/redis

# cp src/redis-server  /usr/redis

# cp src/redis-benchmark /usr/redis

# cp src/redis-cli  /usr/redis

# cp redis.conf  /usr/redis

3、配置Redis

# vim /usr/redis/redis.conf

可參考http://blog.csdn.net/java2000_wl/article/details/8520593

按如下方式修改相關參數:

端口默認:6379

daemonize yes

timeout 300

tcp-keepalive 60

4、啓動Redis服務

# cd /usr/redis

# ./redis-server redis.conf

5、然後用客戶端測試一下是否啓動成功

# ./redis-cli

redis> set “test” “testredis”

OK

redis> get “test”

" testredis "

6.java客戶端的使用

jar包下載地址:http://download.csdn.net/detail/kuailebeihun/8714073

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import play.Play;

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

/**
 * Redis工具類,用於獲取RedisPool. 參考官網說明如下: You shouldn't use the same instance from different threads because you'll have strange errors. And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, which leads to strange errors as well. A single Jedis instance is not threadsafe! To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections. This way you can overcome those strange errors and achieve great performance. To use it, init a pool: JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); You can store the pool somewhere statically, it is thread-safe. JedisPoolConfig includes a number of helpful Redis-specific connection pooling defaults. For example, Jedis with JedisPoolConfig will close a connection after 300 seconds if it has not been returned.
 * 
 * @author bqwang
 */
public class JedisUtil {
	/**
	 * 私有構造器.
	 */
	private JedisUtil() {

	}

	/**
	 * 獲取連接池.
	 * 
	 * @return 連接池實例
	 */
	private static JedisPool getPool(String ip, int port) {
		JedisPool pool = null;
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(Integer.parseInt(Play.configuration.getProperty("redis.pool.maxIdle")));
		config.setMaxTotal(Integer.parseInt(Play.configuration.getProperty("redis.pool.maxTotal")));
		config.setTestOnBorrow(Play.configuration.getProperty("redis.pool.testOnBorrow") == "true" ? true : false);
		config.setTestOnReturn(Play.configuration.getProperty("redis.pool.testOnReturn") == "true" ? true : false);
		try {
			/**
			 * 如果你遇到 java.net.SocketTimeoutException: Read timed out exception的異常信息 請嘗試在構造JedisPool的時候設置自己的超時值. JedisPool默認的超時時間是2秒(單位毫秒)
			 */
			pool = new JedisPool(config, ip, port, Integer.parseInt(Play.configuration.getProperty("redis.pool.timeout")));
		} catch (Exception e) {
			System.out.println("get pool:" + e.getMessage() + "==>" + e.getCause().getMessage());
			e.printStackTrace();
		} finally {
			System.out.println("get pool 獲取連接池.....");
		}
		return pool;
	}

	/**
	 * 類級的內部類,也就是靜態的成員式內部類,該內部類的實例與外部類的實例 沒有綁定關係,而且只有被調用到時纔會裝載,從而實現了延遲加載。
	 */
	private static class RedisUtilHolder {
		/**
		 * 靜態初始化器,由JVM來保證線程安全
		 */
		private static JedisUtil instance = new JedisUtil();
	}

	/**
	 * 當getInstance方法第一次被調用的時候,它第一次讀取 RedisUtilHolder.instance,導致RedisUtilHolder類得到初始化;而這個類在裝載並被初始化的時候,會初始化它的靜 態域,從而創建RedisUtil的實例,由於是靜態的域,因此只會在虛擬機裝載類的時候初始化一次,並由虛擬機來保證它的線程安全性。 這個模式的優勢在於,getInstance方法並沒有被同步,並且只是執行一個域的訪問,因此延遲初始化並沒有增加任何訪問成本。
	 */
	public static JedisUtil getInstance() {
		return RedisUtilHolder.instance;
	}

	/**
	 * 獲取Redis實例.
	 * 
	 * @return Redis工具類實例
	 */
	public Set<String> getJedisData(String key) {
		Set<String> resultSet = null;
		JedisPool pool = null;
		Jedis jedis = null;
		String ip = Play.configuration.getProperty("redis.server.ip");
		int port = Integer.parseInt(Play.configuration.getProperty("redis.server.port"));
		// int retryTimes = Integer.parseInt(Play.configuration.getProperty("redis.pool.retryTimes"));
		// int count = 0;
		// count++;
		try {
			pool = getPool(ip, port);
			jedis = pool.getResource();
			resultSet = jedis.smembers(key);
			// 清空redis中的消息
			jedis.del(key);
		} catch (Exception e) {
			System.out.println("get jedis:" + e.getMessage() + "==>" + e.getCause().getMessage());
			e.printStackTrace();
		} finally {
			// 銷燬對象
			System.out.println("get Jedis ++++++ 銷燬對象");
			if (pool != null) {
				pool.returnBrokenResource(jedis);
			}
		}
		return resultSet;
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章