1.window下使用html
下載地址:http://pan.baidu.com/s/1gdCoKynjava
而後運行redis-server.exelinux
啓動好服務以下,redis
2.打開cmd窗口,cd到目錄C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bitspring
執行:redis-cli.exe(客戶端)shell
3.redis命令-數據存儲介紹數據庫
http://redis.io/topics/data-types-intro vim
官網的例子最好照着作一遍,能快速學會redis,除了最後Bitmap、lexicograpical這些比較難懂,並且也不見得經常使用到,而外基本都是比較通俗易懂的
windows
4.Java訪問redis-jediusapi
jedis-2.7.2.jar下載地址:http://pan.baidu.com/s/1o6mXYIm
給出一個jedius-api:http://tool.oschina.net/uploads/apidocs/
再給一個Java封裝類:
import java.util.ArrayList; import java.util.List; import java.util.Set; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class RedisTool { private static String redisHost = "127.0.0.1"; private static JedisPool pool = new JedisPool(redisHost); // get命令,數據庫默認選擇序號爲0 public static Object get(String key) { Jedis jedis = null; try { jedis = pool.getResource(); byte[] data = jedis.get(key.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // get命令,第二個參數爲選擇數據庫 public static Object get(String key, int dbIndex) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.select(dbIndex); byte[] data = jedis.get(key.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // set命令,默認選擇序號爲0 public static void set(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.set(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // set命令,第二個參數爲選擇數據庫 public static void set(String key, Object value, int dbIndex) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.select(dbIndex); jedis.set(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // hset命令,就是普通的Java-Map對象形式,key標識不一樣的Map對象,field爲指定Map對象的鍵值 public static void hset(String key, String field, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // hget,就是獲取指定key值的Map對象的field字段值 public static Object hget(String key, String field) { Jedis jedis = null; try { jedis = pool.getResource(); byte[] data = jedis.hget(key.getBytes(), field.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // remove爲移除一條記錄 public static void remove(String key) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.del(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // redis實現的自動+1序列獲取 public static long getAutoId(String key) { Jedis jedis = null; long id = 1; try { jedis = pool.getResource(); id = jedis.incr(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } return id; } // list-push,key標識某list對象,並且是left-push public static void lpush(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.lpush(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // list-push,key標識某list對象,並且是right-push public static void rpush(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.rpush(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } } // 獲取指定key值的list對象的全部元素 public static List<Object> lrange(String key) { Jedis jedis = null; List<Object> result = null; try { jedis = pool.getResource(); List<byte[]> list = jedis.lrange(key.getBytes(), 0, -1); result = new ArrayList<Object>(list.size()); for (byte[] o : list) { result.add(SerializeUtil.unserialize(o)); } } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } return result; } // left-pop一個指定key值的list對象 public static Object lpop(String key) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.lpop(key.getBytes()); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } return object; } // right-pop一個指定key值的list對象 public static Object rpop(String key) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.rpop(key.getBytes()); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } return object; } // list-index,查找指定key值的list對象的第index號元素 public static Object lindex(String key, int index) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.lindex(key.getBytes(), index); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { close(jedis); } return object; } // 關閉jedis private static void close(Jedis jedis) { if (jedis != null) { jedis.close(); } } // list-length,獲取指定key值的list對象的元素個數 public static long llen(String key) { Jedis jedis = null; long object; try { jedis = pool.getResource(); object = jedis.llen(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { if (jedis != null) { jedis.close(); } } return object; } // 獲取當前數據庫(默認序號爲0)的全部keys,至關於「keys *」命令 public static List<String> keys() { Jedis jedis = null; List<String> list = null; try { jedis = pool.getResource(); Set<byte[]> set = jedis.keys("*".getBytes()); list = new ArrayList<String>(set.size()); for (byte[] bs : set) { list.add(new String(bs, "UTF8")); } } catch (Exception e) { throw new RuntimeException("Redis出現錯誤!", e); } finally { if (jedis != null) { jedis.close(); } } return list; } }
5.linux下安裝redis
安裝包爲redis-3.0.4.tar.gz,給出下載地址:http://pan.baidu.com/s/1jGm0ncy
先cd到/usr/local/,而後mkdir一個文件夾redis,以後把redis-3.0.4.tar.gz拷貝到/usr/local/redis
解壓,執行tar -zxvf redis-3.0.4.tar.gz
以後cd到redis-3.0.4,執行make命令
而後執行make install命令
完成之後cd到redis-3.0.4,而後執行
[root@localhost redis-3.0.4]# redis-server redis.conf
啓動redis服務,redis服務是部署在210.10.5.189上面,
ip爲210.10.5.102的機器打開cmd窗口,cd到有redis-cli.exe的那個目錄下,C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bit,而後執行redis-cli.exe -h 210.10.5.189 -p 6379,以下
跟windows上部署的沒什麼區別了。
6.還有幾個補充說明
6.1命令
flushdb--刪除當前數據庫中的全部Key flushall--刪除全部數據庫中的key
6.2 linux-redis查看啓動進程:
ps -ef | grep redis
關閉linux的redis服務則kill進程便可
6.3 redis服務後臺啓動,vi redis.conf修改以下,
################################ GENERAL ##################################### # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize no # When running daemonized, Redis writes a pid file in /var/run/redis.pid by # default. You can specify a custom pid file location here. pidfile /var/run/redis.pid # Accept connections on the specified port, default is 6379. # If port 0 is specified Redis will not listen on a TCP socket. port 6379 # TCP listen() backlog.
設置daemonize no->daemonize yes
6.4 linux設置隨機啓動
vi /etc/rc.local
在最後面一行添加:/usr/local/redis/redis-3.0.4/src/redis-server /usr/local/redis/redis-3.0.4/redis.conf
6.5 配置redis最大分配內存
vim redis.conf
修改maxmemory <bytes>爲maxmemory <2147483648>
爲防止內存所有被redis耗光,達到最大內存設置後,Redis會先嚐試清除已到期或即將到期的Key,也會從free-list裏清除一些key-value。當以上方法都沒有效果,那redis就再也不支持寫入操做(如set,lpush),但不影響讀操做如get。(maxmemory配置默認是註釋掉的,沒限制,跟RAM相同)
6.6 存儲策略的配置
這裏官方解釋得很清楚,若是須要修改後當即存儲則第一條記錄修改成
save 1 1
6.7 安全密碼配置(linux下生效,windows下無效,已測試確認)
默認redis是不須要密碼的,若是須要設置密碼爲***則這裏改配置爲
requirepass ***
另外有2篇博客也不錯,主要介紹redis數據結構:
http://blog.csdn.net/liuzhigang1237/article/details/8281487
http://doc.okbase.net/tiantiandjava/archive/124600.html
7.redis主從配置
http://blog.csdn.net/yangzhenzhen/article/details/8512292
8.spring-session-data-redis 把session存到redis裏面加快訪問速度
http://www.bubuko.com/infodetail-825485.html
http://blog.csdn.net/xiejx618/article/details/42919327
9.conf配置