Commit 5bcb2200 authored by Nicholas J Anderson's avatar Nicholas J Anderson
Browse files

Fix collision resolution

Previously, this would just exit if a collision was found since it would
recheck the same short url over and over again.

Additionally, it would not increment the cache counter on successful
short generation since the error would cause the rest of the stanza
to no longer execute
parent c7362984
Pipeline #1251 passed with stage
in 1 minute and 17 seconds
...@@ -134,14 +134,13 @@ class URL(models.Model): ...@@ -134,14 +134,13 @@ class URL(models.Model):
""" """
if cache.get("hashids_counter") is None: if cache.get("hashids_counter") is None:
cache.set("hashids_counter", URL.objects.count()) cache.set("hashids_counter", URL.objects.count())
short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries = 1 tries = 1
while tries < 100: while tries < 100:
try: try:
URL.objects.get(short__iexact=short) short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries += 1 tries += 1
cache.incr("hashids_counter") cache.incr("hashids_counter")
except URL.DoesNotExist as ex: except URL.DoesNotExist as ex:
print(ex) print(ex)
return short return short
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment