StackExchange / StackExchange/StackExchange.Redis

Support key exists from TTL/PTTL command

Open
#2,234 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.2k
Forks
1.6k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

When sending (P)TTL command to redis, the server return the ttl of a key, with two special values:

  • -1 if the key has no expiry
  • -2 if the key does not exists (added in redis 2.8)

https://redis.io/commands/ttl/ & https://redis.io/commands/pttl/

In the library, all negative vvalues are handle the same, and the fact the key does not exists is not handled
https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/ResultProcessor.cs#L318

This means we need to do two calls to redis to check if a key does not have an expire and know if it exists.

I would happily try to post a PR to add this behavior, but not sure on the api change: the library method KeyTimeToLiveAsync currently only returns one TimeSpan

Would adding a new method KeyExistsWithTimeToLiveAsync, returning a struct KeyExistsWithTimeToLiveResult containing a bool and a nullable TimeSpan be ok, adding a RedisFeatures KeyExistsExpiry (>=2.8), and a back up to calling both (P)TTL and EXISTS if feature not available?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TTL result handling in src/StackExchange.Redis/ResultProcessor.cs and the KeyTimeToLiveAsync API in src/StackExchange.Redis/RedisDatabase.cs. Review RedisFeatures and the Redis 2.8 TTL/PTTL semantics before deciding how the API should represent expiry and existence. Done means distinguishing -1 from -2 while preserving a fallback for older servers.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, redis
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.