轉載請標明出處:
http://blog.csdn.net/forezp/article/details/61471712
本文出自方誌朋的博客java
redis是一個開源的,先進的 key-value 存儲可用於構建高性能的存儲解決方案。它支持數據結構有字符串,哈希,列表,集合,帶有範圍查詢的排序集,位圖,超文本和具備半徑查詢的地理空間索引。 NoSQL,Not Only [SQL],泛指非關係型的數據庫。因此redis是一種nosql。敲黑板畫重點:redis是一種nosql.mysql
redis的優勢:linux
linux 系統下安裝:git
$ wget http://download.redis.io/releases/redis-3.2.6.tar.gz $ tar xzf redis-3.2.6.tar.gz $ cd redis-3.2.6 $ make
啓動服務器:github
$ src/redis-server
啓動客戶端web
$ src/redis-cli
mac下安裝:redis
brew install redis
啓動:sql
redis-server redis-cli
windows下安裝:mongodb
因爲官方並無提供windows 版本,不過微軟爲了可以應用redis 到 windows服務器,由微軟維護了windows版的redis,下載地址:點擊進入.建議下載msi 版本,直接安裝便可。數據庫
啓動成功:
[35142] 01 May 14:36:28.939 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf [35142] 01 May 14:36:28.940 * Max number of open files set to 10032 _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.6.12 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 35142 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [35142] 01 May 14:36:28.941 # Server started, Redis version 2.6.12 [35142] 01 May 14:36:28.941 * The server is now ready to accept connections on port 6379
啓動客戶端 ,存儲字符串到redis.
redis> SET name forezp OK
取字符串:
redis> get name "forezp"
redis > HMSET king username forezp password xxdxx age 22 redis > HGETALL king 1) "username" 2) "forezp " 3) "password " 4) "xxdxx " 5) "age " 6) "22"
redis> lpush pricess jack (integer) 1 redis 127.0.0.1:6379> lpush pricess jolin (integer) 2 redis 127.0.0.1:6379> lpush pricess mayun (integer) 3 redis 127.0.0.1:6379> lrange pricess 0 10 1) "jack" 2) "jolin" 3) "mayun"
Redis有序集合相似Redis集合存儲在設定值惟一性。不一樣的是,一個有序集合的每一個成員帶有分數,用於以便採起有序set命令,從最小的到最大的分數有關。
redis > ZADD kindom 1 redis (integer) 1 redis> ZADD kindom 2 mongodb (integer) 1 redis > ZADD kindom 3 mysql (integer) 1 redis > ZADD kindom 3 mysql (integer) 0 redis > ZADD kindom 4 mysql (integer) 0 redis > ZRANGE kindom 0 10 WITHSCORES 1) "redis" 2) "1" 3) "mongodb" 4) "2" 5) "mysql" 6) "4"
開啓客戶端做爲接受者
redis> SUBSCRIBE myking messages... (press Ctrl-C to quit )1) "subscribe" 2) "myking " 3) (integer) 1
開啓另外一個客戶端做爲發送者:
redis > PUBLISH myking "Redis is a great caching technique" (integer) 1
這樣接受者就能夠收到:
"Redis is a great caching technique"
1.獲取因此的key
redis> KEYS *
2,判斷key是否存在
EXISTS key
3.刪除key
DEL key [key …]
4.獲取數據類型
TYPE key
5.向尾部添加
APPEND key value
6.獲取字符串長度
strlen key
固然這裏只是介紹簡單的一些操做,複雜的參考官方文檔。
前提是redis 已經安裝,而且已經開啓服務。
jedis 下載地址 https://github.com/xetorthio/jedis
Jedis is a blazingly small and sane Redis java client.
Jedis was conceived to be EASY to use.
翻譯: jedis是一個很是小的java客戶端,被認爲是容易使用。
怎麼使用?
public static void main(String[] args){ Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); //check whether server is running or not System.out.println("Server is running: "+jedis.ping()); jedis.lpush("forezp-list", "Redis"); jedis.lpush("forezp-list", "Mongodb"); jedis.lpush("forezp-list", "Mysql"); // Get the stored data and print it List<String> list = jedis.lrange("forezp-list", 0 ,5); for(int i=0; i<list.size(); i++) { System.out.println("Stored string in redis:: "+list.get(i)); } }
運行:
Connection to server sucessfully
Server is running: PONG
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis
redis 入門介紹就到這裏了。另外,敲黑板,劃重點: 遇到問題首先不要去百度搜,要去官網搜。聰明的你,是否是本身安裝下 ,實踐下。
掃碼關注公衆號有驚喜
(轉載本站文章請註明做者和出處 方誌朋的博客)