redis的簡單使用和介紹 linux(centos 5.4) redis install

來源:互聯網
上載者:User

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.gztar zxvf redis-2.0.4.tar.gzcd  redis-2.0.4make

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);}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.