Skip to content

Commit 263cf76

Browse files
author
Chris Rossi
authored
fix: limit memcache keys to 250 bytes (#663)
Fixes #619
1 parent d9da7f5 commit 263cf76

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

packages/google-cloud-ndb/google/cloud/ndb/global_cache.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import abc
1818
import base64
1919
import collections
20+
import hashlib
2021
import os
2122
import pymemcache.exceptions
2223
import redis.exceptions
@@ -470,7 +471,10 @@ def _parse_host_string(host_string):
470471

471472
@staticmethod
472473
def _key(key):
473-
return base64.b64encode(key)
474+
encoded = base64.b64encode(key)
475+
if len(encoded) > 250:
476+
encoded = hashlib.sha1(encoded).hexdigest()
477+
return encoded
474478

475479
@classmethod
476480
def from_environment(cls, max_pool_size=4, strict_read=False, strict_write=True):

packages/google-cloud-ndb/tests/unit/test_global_cache.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ def test_clear():
354354

355355

356356
class TestMemcacheCache:
357+
@staticmethod
358+
def test__key_long_key():
359+
key = b"ou812" * 100
360+
encoded = global_cache.MemcacheCache._key(key)
361+
assert len(encoded) == 40 # sha1 hashes are 40 bytes
362+
357363
@staticmethod
358364
@mock.patch("google.cloud.ndb.global_cache.pymemcache")
359365
def test_from_environment_not_configured(pymemcache):

0 commit comments

Comments
 (0)