Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> ฐานข้อมูล

Mastering Redis Hashes: Fast, Flexible Data Storage

Hashes ใน Redis = Store Associated Field-Value Pairs Under Single Key Field และ Values เป็น Strings Redis Allow Modifications Data Structure As Whole และ Each Field ใน Structure ทำให้เป็น Great (Very Fast) Backing Store สำหรับ Objects ใน Application

CLI Examples

Create Hash 2 Fields:

127.0.0.1:6379> HMSET my_hash key1 "foo" key2 "bar" OK

List Fields/Values:

127.0.0.1:6379> HGETALL my_hash 1) "key1" 2) "foo" 3) "key2" 4) "bar"

Update Field Value:

127.0.0.1:6379> HSET my_hash key1 "xyzzy" (integer) 0 127.0.0.1:6379> HGET my_hash key1 "xyzzy"

Delete Single Field:

127.0.0.1:6379> HDEL my_hash key2 (integer) 1 127.0.0.1:6379> HGETALL my_hash 1) "key1" 2) "xyzzy"

Delete Hash:

127.0.0.1:6379> DEL my_hash (integer) 1 127.0.0.1:6379> HGETALL my_hash (empty list or set)

Redis Documentation มี Commands Hashes เยอะ ไม่ Copy/Paste แทนจะ Have Fun กับมัน

Dwemthy's Array!

Dwemthy's Array = Mini RPG Example โดย Why the Lucky Stiff ใน Why's (poignant) Guide to Ruby อธิบาย Ruby Metaprogramming ที่นี่ Implement Same RPG Idea ด้วย Redis Hashes (Help จาก Redis Array)

Create Hash Per Monster In Array:

name: "AssistantViceTentacleAndOmbudsman" life: 320 strength: 6 charisma: 144 weapon: 50

Example Dive:

>>> import redis >>> import dwemthy >>> conn = redis.StrictRedis(host="localhost", port=6379) >>> dw = dwemthy.Array.new(conn, { "name": "AssistantViceTentacleAndOmbudsman", "life": 320, "strength": 6, "charisma": 144, "weapon": 50 } ) "[Get ready. AssistantViceTentacleAndOmbudsman has emerged!]" >>> rabbit = dwemthy.Rabbit(dw) >>> rabbit.sword() "[You hit with 2 points of damage!]" "[Your enemy hit with 10 points of damage!]" "[Rabbit has died!]"

Full Code: dwemthy.py

Code Highlights

User Creates Array Factory Method -> Setup Data Structures Return New dwemthy.Array Object:

class Array(object): list_key = "dwemethys_array" ... @classmethod def new(cls, conn, *bad_guys): """ Create a new set of problems for our hero to boomerang! """ conn.delete(cls.list_key) # Give each bad guy a cozy spot in the array! for bad_guy in bad_guys: key = uuid.uuid4() conn.hmset(key, bad_guy) conn.rpush(cls.list_key, key) dw_list = cls(conn) dw_list.next_enemy() return dw_list

conn.hmset Creates Hash Bad Guy Push Bad Guy Key Into Redis List (Simple Queue)

All Bad Guys Setup -> Ensure Head Of Array Ready Array.next_enemy():

class Array(object): list_key = "dwemethys_array" ... def next_enemy(self): """ Bring out the next enemy for our rabbit to face! """ # We're moving on! if self.current != None: self.conn.delete(self.current) enemy_key = self.conn.lpop(self.list_key) if enemy_key is None: # Like a boss! print "[Whoa. You decimated Dwemthy's List!]" else: # Get the new enemy ready! self.current = enemy_key print "[Get ready. {} has emerged!]".format(self["name"])

self["name"] Last Line = Define __setitem__ __getitem__ Methods Array Object Treat Array As Python Dict Easy Fetch/Modify Fields Current Bad Guy:

class Array(object): ... def __setitem__(self, key, value): self.conn.hset(self.current, key, value) def __getitem__(self, key): value = self.conn.hget(self.current, key) # hashes only store strings! if key != "name": return int(value) return value

Conclusion

Myriad Ways Redis Modify Hashes Enable Developers Solve Large Set Problems Hopefully Post Gives Ideas Own Projects