Geek Academy Week 3 :: Redis for developer

  • 1,570 views
Uploaded on

Geek Academy Week 3 :: Redis for developer …

Geek Academy Week 3 :: Redis for developer
- Redis
- With Java Library => Jedis
- With Spring Data :: Redis

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,570
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
58
Comments
0
Likes
14

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. REDIS For Developer 3rd WEEK SATURDAY, JULY 06, 2013 – SUNDAY, JULY 07, 2013 GEEK ACADEMY 2013
  • 2. INSTRUCTOR TEAM TAVEE SOMKIAT THAWATCHAI Tavee (Vee) Khunbida Somkiat (Pui) Puisungnuen Thawatchai (Boy) Jongsuwanpaisan Siam Chamnan Kit tavee@sprint3r.com Siam Chamnan Kit somkiat@sprint3r.com Siam Chamnan Kit thawatchai@sprint3r.com
  • 3. WHAT IS REDIS ? o REmote DIrectory Server o By Salvatore Sanfilippo (@antirez) working at VMWare o March 2009 o redis.io
  • 4. WHAT IS REDIS ? o REmote DIrectory Server o NOSQL => Key-Value o Data Structure Server
  • 5. NOSQL ( Not Only SQL ) o Key-Value Store o Document Database o Column Store o Graph Database http://www.10gen.com/nosql
  • 6. NOSQL ( Not Only SQL ) o Key-Value Store o Redis, Riak, Memcached, Tokyo Cabinet, LevelDB o Document Database o MongoDB, CouchDB, RethinkDB o Column Family Store o Cassandra, Hadoop, HBase o Graph Database o Neo4J, HyperGraphDB, InfoGrid
  • 7. WHY WE USE REDIS ? o Very Fast ( In memory ) o Few Dependencies o Single Thread o Lot of client available o Java o Ruby o PHP o Python o .Net
  • 8. FEATURES o Values Type o String o List o Set o Hash o Sorted Set o Persistence o Memory o Snapshot o Append Only Log ( AOL )
  • 9. FEATURES o Replication o Master-Slave o Pub-Sub o Clustering ( Beta version )
  • 10. WHO USE REDIS ? http://redis.io/topics/whos-using-redis
  • 11. REDIS IN PRACTICE o View Count o Presence o Activity Feed o Suggestion o Caching o Tracking o Logging o ….
  • 12. DATA STRUCTURE GEEK ACADEMY 2013
  • 13. List o List of String o Sorted by insertion order o Max length of List = 232 -1 o Big O o Add operation = O(1) o Access operation = O(S+N) o Update = O(1) o Delete = O(1)
  • 14. Set o Unordered collections of String o Not repeat members o Max length of List = 232 -1 o Operation => Union, Intersection, Different o Big O o Add operation = O(N) o Access operation = O(N) o Update = O(1) o Delete = O(1) o Exists = O(1)
  • 15. Sorted Set o Like Set BUT … o Ordered collections of String by Score o Big O o Add operation = O(log(N)) o Access operation = O(log(N)+M) o Delete = O(M*log(N))
  • 16. Hash o Mapping Key-Value o Max length of List = 232 -1 o Big O o Add operation = O(N) o Access operation = O(1) o Delete = O(1)
  • 17. Summary
  • 18. INSTALLATION FOR DEV GEEK ACADEMY 2013
  • 19. FOR WINDOWS o Download at o https://github.com/dmajkic/redis/downloads o Commands o redis-server.exe o redis-cli.exe o redis-benchmark
  • 20. FOR UNIX $ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz $ tar xzf redis-2.6.14.tar.gz $ cd redis-2.6.14 $ make Starting server $ src/redis-server Interact with Redis $ src/redis-cli
  • 21. FOR MAC o Up to you
  • 22. INSTALLATION FOR DEVOPS GEEK ACADEMY 2013
  • 23. VAGRANT + PUPPET o OS = Ubuntu o Configure repository of Redis o Install with apt-get
  • 24. Configure repository
  • 25. Install and Starting Redis Service
  • 26. Install on Virtual Machine $vagrant up $vagrant reload $vagrant provision
  • 27. BASIC USAGE GEEK ACADEMY 2013
  • 28. LET’S DEMO o redis-cli o http://try.redis.io/ o http://redis.io/commands
  • 29. DEMO 1 o PING o INFO o KEYS o EXISTS o DEL o FLUSHDB
  • 30. DEMO 2 :: Working with String o SET o GET o GETRANGE o APPEND
  • 31. DEMO 3 :: Counter o INCR o INCRBY o DECR o DECRBY
  • 32. DEMO 4 :: Working with List o LPUSH o RPUSH o LRANGE o LTRIM o LLEN
  • 33. DEMO 5 :: Working with Set o SADD o SMEMBERS o SINTER o SPOP o SRANDMEMBER o SUNIONSTORE
  • 34. DEMO 6 :: Working with Sorted Set o ZADD o ZRANGE o ZRANGEBYSCORE o ZRANK o ZINCRBY o ZREVRANGE
  • 35. DEMO 7 :: Working with Hash o HSET o HGET o HGETALL o HDEL o HEXISTS o HLEN o HMSET o HMGET
  • 36. REDIS#VIEW COUNT GEEK ACADEMY 2013
  • 37. View count with Redis
  • 38. REDIS#PRESENCE GEEK ACADEMY 2013
  • 39. Presence with Redis o Presence = Who’s Online ? o What’s Data Structure ?
  • 40. Presence with Redis Online Users Friends
  • 41. Data Structure ? o List o Set o Sorted Set
  • 42. Implementation ? o Set o Define Key o Online user o Key = user:online:<time> o Value = <user id> o Friends o Key = user:friend:<user id> o Value = <user id>
  • 43. Implementation ? o Online Users o sadd user:online:1 1 o sadd user:online:1 2 o sadd user:online:1 3 o sadd user:online:2 3 o sadd user:online:2 4
  • 44. Implementation ? o Friends by User o sadd user:friend:1 2 o sadd user:friend:1 3
  • 45. Implementation ? o Find Friends are online in last 2 minutes o sunionstore online_users user:online:1 user:online:2 o sinter online_users user:friend:1
  • 46. Presence with Redis
  • 47. REDIS#SUGGESTION GEEK ACADEMY 2013
  • 48. Keyword Suggestion with Redis
  • 49. DBRMS Solution o SELECT KEYWORD o FROM SOME_TABLE o WHERE LOWER(KEYWORD) LIKE “redis%”
  • 50. Redis Solution o What is Data Structure ? o List o Set o Sorted Set
  • 51. Implementation ? o Sorted Set o Example => redis o r o re o red o redi o redis
  • 52. Specified word o Example => redis o r o re o red o red* o redi o redis o redis*
  • 53. Algorithm o Called N-gram http://en.wikipedia.org/wiki/N-gram http://books.google.com/ngrams/
  • 54. Add data to Redis Sorted Set o zadd my_data 0 r o zadd my_data 0 re o zadd my_data 0 red o zadd my_data 0 red* o zadd my_data 0 redi o zadd my_data 0 redis o zadd my_data 0 redis*
  • 55. List of data in Sorted Set o zrange my_data 0 -1 o zrank my_data r o zrank my_data re
  • 56. How to apply in application ? o Let’s discussion
  • 57. REDIS#CACHING GEEK ACADEMY 2013
  • 58. Caching o Denormalization Data o Generalization Cache with LRU, TTL
  • 59. Redis :: Memory policy o Volatile-LRU o Volatile-TTL o Volatile-Random o AllKeys-LRU o AllKeys-Random o NoEviction
  • 60. WORKING WITH JAVA GEEK ACADEMY 2013
  • 61. Redis Client Library o Jedis o JRedis o JDBC-Redis o RJC o Redis-protocol o Lettuce http://redis.io/clients
  • 62. For Traditional Developer o Goto web o Search and Download o Copy to build path of project !!!! o Not for geek ….
  • 63. For Geek o Use Maven or Gradle
  • 64. Jedis o https://github.com/xetorthio/jedis o Maven Dependency <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope> </dependency>
  • 65. Jedis :: Easy to use Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo");
  • 66. WORKING WITH SPRING FRAMEWORK GEEK ACADEMY 2013
  • 67. Let’s go o We use Spring Data with Redis o http://www.springsource.org/spring-data/redis
  • 68. Working with Maven <repository> <id>spring-release</id> <name>Spring Maven RELEASE Repository</name> <url>http://maven.springframework.org/release</url> </repository> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.5.RELEASE</version> </dependency>
  • 69. Working with Annotation @Configuration public class RedisConfig { @Bean JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(redisHost); jedisConnectionFactory.setPort(redisPort); return jedisConnectionFactory; } @Bean RedisTemplate<String, String> redisTemplate() { final RedisTemplate<String, String> template = new RedisTemplate<String, String>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class)); template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class)); return template; } }
  • 70. Using RedisTemplate in Repository @Repository public class LoggingDaoImpl implements LoggingDao { private final static String LOGGING_KEY = "logging"; @Autowired RedisTemplate<String, String> redisTemplate; public void addData(String key, String value) { redisTemplate.opsForList().rightPush(LOGGING_KEY, key); redisTemplate.opsForValue().set(key, value); System.out.println( LOGGING_KEY ); System.out.println( key ); } }
  • 71. See more o https://github.com/up1/geeky_week3_demo o https://github.com/up1/Spring-Framework- Demo/tree/master/SpringRedis
  • 72. Question ?
  • 73. GEEK ACADEMY 2013
  • 74. THANK YOU FOR YOUR TIME GEEK ACADEMY 2013