Skip to content

Commit d5cd234

Browse files
committed
Used hinctby for impressions counts
1 parent 921bf6e commit d5cd234

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

splitio/engine/impressions/adapters.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def __init__(self, redis_client):
6666
:type telemtry_http_client: splitio.api.telemetry.TelemetryAPI
6767
"""
6868
self._redis_client = redis_client
69+
self.pipe = self._redis_client.pipeline()
70+
6971

7072
def record_unique_keys(self, uniques):
7173
"""
@@ -88,13 +90,14 @@ def flush_counters(self, to_send):
8890
"""
8991
post the impression counters to redis.
9092
91-
:param uniques: unique keys disctionary
92-
:type uniques: Dictionary {'feature1': set(), 'feature2': set(), .. }
93+
:param to_send: unique keys disctionary
94+
:type to_send: Dictionary {'feature1': set(), 'feature2': set(), .. }
9395
"""
94-
bulk_counts = self._build_counters(to_send)
9596
try:
96-
inserted = self._redis_client.rpush(self.IMP_COUNT_QUEUE_KEY, bulk_counts)
97-
self._expire_keys(self.IMP_COUNT_QUEUE_KEY, self.IMP_COUNT_KEY_DEFAULT_TTL, inserted, len(to_send))
97+
for pf_count in to_send:
98+
self.pipe.hincrby(self.IMP_COUNT_QUEUE_KEY, pf_count.feature + "::" + str(pf_count.timeframe), pf_count.count)
99+
result = self.pipe.execute()
100+
self._expire_keys(self.IMP_COUNT_QUEUE_KEY, self.IMP_COUNT_KEY_DEFAULT_TTL, result[0], pf_count.count)
98101
return True
99102
except RedisAdapterException:
100103
_LOGGER.error('Something went wrong when trying to add counters to redis')

splitio/storage/adapters/redis.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ def hget(self, name, key):
241241
except RedisError as exc:
242242
raise RedisAdapterException('Error executing hget operation') from exc
243243

244+
def hincrby(self, name, key, amount=1):
245+
"""Mimic original redis function but using user custom prefix."""
246+
try:
247+
return self._decorated.hincrby(self._prefix_helper.add_prefix(name), key, amount)
248+
except RedisError as exc:
249+
raise RedisAdapterException('Error executing hincrby operation') from exc
250+
244251
def incr(self, name, amount=1):
245252
"""Mimic original redis function but using user custom prefix."""
246253
try:
@@ -323,6 +330,10 @@ def incr(self, name, amount=1):
323330
"""Mimic original redis function but using user custom prefix."""
324331
self._pipe.incr(self._prefix_helper.add_prefix(name), amount)
325332

333+
def hincrby(self, name, key, amount=1):
334+
"""Mimic original redis function but using user custom prefix."""
335+
self._pipe.hincrby(self._prefix_helper.add_prefix(name), key, amount)
336+
326337
def execute(self):
327338
"""Mimic original redis function but using user custom prefix."""
328339
try:

0 commit comments

Comments
 (0)