question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method keys_match() returns different keys types for Redis and memory

See original GitHub issue

Method keys_match() returns keys in bytes type for Redis cache and in str type for memory cache. Is it a bug or feature?

Code example:

# setup two caches - for redis & memory
cache.setup(f'redis://{host}:{port}', password=pwd, prefix='test_redis')
cache.setup('mem://', prefix='test_mem')

# set values for memory and redis
await cache.set('test_redis:abc', 123)
await cache.set('test_mem:abc', 321)

# keys_match result
async def foo(mask):
  async for key in cache.keys_match(mask):
    print(key, type(key))

await foo('test_redis:*')
# b'test_redis:abc' <class 'bytes'>
await foo('test_mem:*')
# test_mem:abc <class 'str'>

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
AIGeneratedUsernamecommented, May 9, 2022

I’m affraid it will be a breaking change for some users in case of changing result type of keys_match by default.

That is why everybody must pin their requirements. And have a full tests coverage, of course. We can add a DeprecationWarning for the method, and update it on the next major release. Win-win, all will be happy. As a temporary workaround, another method may be added.

@AIGeneratedUsername Would you like to remake you MR with a new method that fix this issue?

@Krukov Do you mean creating a method with a new name? Will this method do the same as keys_match for the “Memory” and “Disk” backends, but return strings for the “Redis” backend? Simplified pseudo-code:

class MemoryBackend:
    keys = keys_match  # Or `scan = keys_match`

class DiskBackend:
    keys = keys_match  # Or `scan = keys_match`

class RedisBackend:
    async def keys():  # Or name the method as `scan` to be consistent with Redis terminology
           # yield keys as strings...
           # technically, we can call `keys_match` and yield from it `key.decode()`
1reaction
AIGeneratedUsernamecommented, May 6, 2022

The “Memory” cache is a dictionary with strings as keys. The input key is a string, and it is stored as a string and returned as a string. It is never converted or stored as bytes. What you are describing is expected behavior.

Maybe adding type hinting for the whole package code will simplify the package usage. It may be more clear right away what type to expect before running the code.

Any better ideas or proposals are welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redis data types tutorial
Learning the basic Redis data types and how to use them. ... The ability to set or retrieve the value of multiple keys...
Read more >
Find and Delete multiple keys matching by a pattern in Redis
Use scanstream to find and pipeline to delete keys matching pattern.Find and Delete multiple keys matching by a pattern in Redis in a...
Read more >
Redis — An Introduction - Runtime Revolution
Redis is an in-memory key-value data store that you can integrate with your application to store volatile data. It's volatile because once ...
Read more >
Redis Clustering Best Practices with Multiple Keys
Redis quickly stretched this concept with data types, where a single key could refer to multiple (even millions of) pieces of data.
Read more >
How to obtain size in bytes for set of keys in Redis?
Is there a functionality similar to like sizeof(keysmatch:*)? NO. AFAIK, you have to use MEMORY USAGE . Is there a functionality that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found