Redis X Javaå ¥éãRedisã®ã¤ã³ã¹ãã¼ã«ããã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªJedisã®ä½¿ãæ¹ã¾ã§
Redistã¨ã¯ãæ°¸ç¶åæ©è½ãåããKVSã§ãã
http://redis.io/
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯memcachedã¨ã»ã¼åãã§ãããRedisã®æ¹ãæ°¸ç¶åãå«ãã¦ã§ãããã¨ãå¤ãã®ã§æè¦ã§ããã¨ããã¯ããããããããã§ãã
ã¨ãããããco-meetingã®ã»ãã·ã§ã³ç®¡çã«ä½¿ããã¨ã«ããã®ã§ãHello Worldã¾ã§ã®ã¡ã¢ãæ®ãã¦ããã¾ãã
ã¤ã³ã¹ãã¼ã«æ¹æ³
2011å¹´9æ1æ¥ç¾å¨ã®Ubuntu 10.10ã®ãªãã¸ããªã§ã¤ã³ã¹ãã¼ã«ãããRedisã®ãã¼ã¸ã§ã³ã¯2.0.0-rc2-1ã§ãã
rcãªã®ãæ°ã«ãªãã¾ãããå¤ãã¯ãªãã®ã§ã¨ããããapt-getã§ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ããã
$ãsudo apt-get install redis-server
telnetã§åä½ã確èª
Redisã®åä½ãããã«ç¢ºèªããã«ã¯ãtelnetã§ã³ãã³ããæã¤ã®æã£åãæ©ãã§ãã
http://try.redis-db.com/ã§ãã³ãã³ãã試ãã¾ãã
ã³ãã³ãã®ä¸è¦§ã¯ãã¡ã
http://redis.io/commands
æååã®è¿½å /åé¤ãtelnetãã試ãã¦ã¿ã¾ããRedisã®ããã©ã«ããã¼ãã¯6379ã§ãã
$ telnet localhost 6379
set mykey 5 <- setã³ãã³ãã§æååãä¿åãã¾ãããã©ã¡ã¼ã¿ã«æååé·ãæå®ãã¦ãã¾ããã Hello æ°ãããã¼ã¸ã§ã³ã§ã¯ç´æ¥æååãæå®ã§ããããã«ãªã£ã¦ãã¾ãã(http://redis.io/commands/set) +OK keys * <- keysã³ãã³ãã¯ä¿åããã¦ãããã¼ã確èªãããã¨ãã§ãã¾ãã *1ãããããããããããã§ã¯ãã¹ã¦ã®ãã¼ã表示ãã¦ãã¾ãã(http://redis.io/commands/keys) $5 mykey get mykey <- getã«ãã¼ãæå®ãã¦æååãåå¾ã(http://redis.io/commands/get) $5 Hello del mykey <- getã§åé¤ã(http://redis.io/commands/del) :1 keys * *0
Javaã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãª
ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã¯é常ã«ããããããã¾ãã
http://redis.io/clients
Javaã ãã§ã4ã¤
- Jedis: â Repository xetorthio
- JRedis: Repository Homepage SunOf27
- JDBC-Redis: Repository Homepage mavcunha
- RJC: Repository e_mzungu
ã¹ã¿ã¼ãã¤ãã¦ããã®ãããããã ãããªã®ã§ãä»åãããããã«ããªã£ã¦ããJedisã試ãã¦ã¿ã¾ããã
ã¾ããstackoverflowã®QAãBest Redis library for Javaãã§ããJDBCã¯é ãããJedisãJRedisã使ã£ãæ¹ãããã¨æ¸ãã¦ããã¾ãã
Jedisã®å ¥æã¨ç°å¢æ§ç¯
Jedisã¯Githubã«ã¦å
¬éããã¦ãã¾ãã
https://github.com/xetorthio/jedis
[Downloads]ããjedis-2.0.0.jarããã¦ã³ãã¼ãã§ãã¾ãã
Jedisã®pom.xmlãè¦ãã¨ä»¥ä¸ã®2ã¤ä¾åã©ã¤ãã©ãªãããã¾ãã
- junit 4.8.1
- commons-pool 1.5.5
å®è¡æã«ã¯commons-poolã®ã¿ã¯ã©ã¹ãã¹ã«å«ããå¿ è¦ãããã¾ãã
Jedisã®ä½¿ãæ¹
ã©ã¤ãã©ãªã®ä½¿ãæ¹ã¯Wikiã«è©³ããè¼ã£ã¦ãã¾ãã
telnetã§è©¦ããæä½ãä¸éãåç¾ããã¨ãããªæãã§ãã
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class HelloJedis { /** * @param args */ public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); Jedis jedis = pool.getResource(); try { jedis.set("mykey", "Hello"); System.out.println(jedis.keys("*")); System.out.println(jedis.get("mykey")); jedis.del("mykey"); System.out.println(jedis.keys("*")); } finally { pool.returnResource(jedis); } pool.destroy(); } }
å®è¡çµæ
[mykey] Hello []