Skip to content

Commit

Permalink
Improve Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTung97 committed Dec 1, 2023
1 parent d4ef3d5 commit 62680fb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion memproxy/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,17 @@ def result(self) -> LeaseGetResponse:
self.pipe.execute(state)

if state.redis_error is not None:
# release to pool
if len(_P) < 4096:
_P.append(self)
return 3, b'', 0, f'Redis Get: {state.redis_error}'

get_resp = state.get_result[self.index]

# release to pool
if len(_P) < 4096:
_P.append(self)

if get_resp.startswith(b'val:'):
return 1, get_resp[len(b'val:'):], 0, None

Expand All @@ -205,6 +212,9 @@ def result(self) -> LeaseGetResponse:
return 1, get_resp, 0, None


_P: List[_RedisGetResult] = []


class RedisPipeline:
__slots__ = ('client', 'get_script', 'set_script', '_sess',
'_min_ttl', '_max_ttl', 'max_keys_per_batch',
Expand Down Expand Up @@ -264,7 +274,12 @@ def lease_get(self, key: str) -> LeaseGetResult:
index = len(state.keys)
state.keys.append(key)

result = _RedisGetResult()
# do init get result
try:
result = _P.pop()
except IndexError:
result = _RedisGetResult()

result.pipe = self
result.state = state
result.index = index
Expand Down

0 comments on commit 62680fb

Please sign in to comment.