|
| 1 | +# EXAMPLE: pipe_trans_tutorial |
| 2 | +# HIDE_START |
| 3 | +""" |
| 4 | +Code samples for vector database quickstart pages: |
| 5 | + https://redis.io/docs/latest/develop/get-started/vector-database/ |
| 6 | +""" |
| 7 | +# HIDE_END |
| 8 | +import redis |
| 9 | + |
| 10 | +# STEP_START basic_pipe |
| 11 | +r = redis.Redis(decode_responses=True) |
| 12 | +# REMOVE_START |
| 13 | +for i in range(5): |
| 14 | + r.delete(f"seat:{i}") |
| 15 | + |
| 16 | +r.delete("shellpath") |
| 17 | +# REMOVE_END |
| 18 | + |
| 19 | +pipe = r.pipeline() |
| 20 | + |
| 21 | +for i in range(5): |
| 22 | + pipe.set(f"seat:{i}", f"#{i}") |
| 23 | + |
| 24 | +set_5_result = pipe.execute() |
| 25 | +print(set_5_result) # >>> [True, True, True, True, True] |
| 26 | + |
| 27 | +pipe = r.pipeline() |
| 28 | + |
| 29 | +# "Chain" pipeline commands together. |
| 30 | +get_3_result = pipe.get("seat:0").get("seat:3").get("seat:4").execute() |
| 31 | +print(get_3_result) # >>> ['#0', '#3', '#4'] |
| 32 | +# STEP_END |
| 33 | +# REMOVE_START |
| 34 | +assert set_5_result == [True, True, True, True, True] |
| 35 | +assert get_3_result == ['#0', '#3', '#4'] |
| 36 | +# REMOVE_END |
| 37 | + |
| 38 | +# STEP_START trans_watch |
| 39 | +r.set("shellpath", "/usr/syscmds/") |
| 40 | + |
| 41 | +with r.pipeline() as pipe: |
| 42 | + # Repeat until successful. |
| 43 | + while True: |
| 44 | + try: |
| 45 | + # Watch the key we are about to change. |
| 46 | + pipe.watch("shellpath") |
| 47 | + |
| 48 | + # The pipeline executes commands directly (instead of |
| 49 | + # buffering them) from immediately after the `watch()` |
| 50 | + # call until we begin the transaction. |
| 51 | + current_path = pipe.get("shellpath") |
| 52 | + new_path = current_path + ":/usr/mycmds/" |
| 53 | + |
| 54 | + # Start the transaction, which will enable buffering |
| 55 | + # again for the remaining commands. |
| 56 | + pipe.multi() |
| 57 | + |
| 58 | + pipe.set("shellpath", new_path) |
| 59 | + |
| 60 | + pipe.execute() |
| 61 | + |
| 62 | + # The transaction succeeded, so break out of the loop. |
| 63 | + break |
| 64 | + except redis.WatchError: |
| 65 | + # The transaction failed, so continue with the next attempt. |
| 66 | + continue |
| 67 | + |
| 68 | +get_path_result = r.get("shellpath") |
| 69 | +print(get_path_result) # >>> '/usr/syscmds/:/usr/mycmds/' |
| 70 | +# STEP_END |
| 71 | +# REMOVE_START |
| 72 | +assert get_path_result == '/usr/syscmds/:/usr/mycmds/' |
| 73 | +r.delete("shellpath") |
| 74 | +# REMOVE_END |
| 75 | + |
| 76 | +# STEP_START watch_conv_method |
| 77 | +r.set("shellpath", "/usr/syscmds/") |
| 78 | + |
| 79 | + |
| 80 | +def watched_sequence(pipe): |
| 81 | + current_path = pipe.get("shellpath") |
| 82 | + new_path = current_path + ":/usr/mycmds/" |
| 83 | + |
| 84 | + pipe.multi() |
| 85 | + |
| 86 | + pipe.set("shellpath", new_path) |
| 87 | + |
| 88 | + |
| 89 | +trans_result = r.transaction(watched_sequence, "shellpath") |
| 90 | +print(trans_result) # True |
| 91 | + |
| 92 | +get_path_result = r.get("shellpath") |
| 93 | +print(get_path_result) # >>> '/usr/syscmds/:/usr/mycmds/' |
| 94 | +# REMOVE_START |
| 95 | +assert trans_result |
| 96 | +assert get_path_result == '/usr/syscmds/:/usr/mycmds/' |
| 97 | +# REMOVE_END |
| 98 | +# STEP_END |
0 commit comments