{"record":{"id":"633fbf372676813e","repo":"App-vNext/Polly","slug":"the-ttl-for-items-to-cache-must-be-greater-than-ze-633fbf","errorCode":null,"errorMessage":"The ttl for items to cache must be greater than zero.","messagePattern":"The ttl for items to cache must be greater than zero\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Polly/Caching/SlidingTtl.cs","lineNumber":19,"sourceCode":"﻿#nullable enable\nnamespace Polly.Caching;\n\n/// <summary>\n/// Defines a ttl strategy which will cache items with a sliding ttl.\n/// </summary>\npublic class SlidingTtl : ITtlStrategy\n{\n    private readonly Ttl _ttl;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"SlidingTtl\"/> class.\n    /// </summary>\n    /// <param name=\"slidingTtl\">The sliding timespan for which cache items should be considered valid.</param>\n    public SlidingTtl(TimeSpan slidingTtl)\n    {\n        if (slidingTtl < TimeSpan.Zero)\n        {\n            throw new ArgumentOutOfRangeException(nameof(slidingTtl), \"The ttl for items to cache must be greater than zero.\");\n        }\n\n        _ttl = new Ttl(slidingTtl, true);\n    }\n\n    /// <summary>\n    /// Gets a TTL for the cacheable item.\n    /// </summary>\n    /// <param name=\"context\">The execution context.</param>\n    /// <param name=\"result\">The execution result.</param>\n    /// <returns>A <see cref=\"Ttl\"/> representing the remaining Ttl of the cached item.</returns>\n    public Ttl GetTtl(Context context, object? result) =>\n        _ttl;\n}\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Caching/SlidingTtl.cs#L1-L34","documentation":"Thrown by the legacy Polly v7 SlidingTtl constructor when the supplied TimeSpan is less than TimeSpan.Zero. SlidingTtl is an ITtlStrategy that marks cache entries with a sliding TTL; a negative sliding window is invalid, so Polly throws ArgumentOutOfRangeException. As with RelativeTtl, TimeSpan.Zero passes but yields an instantly-expiring entry.","triggerScenarios":"Constructing new SlidingTtl(negativeTimeSpan); a sliding duration derived from configuration or computation that went negative.","commonSituations":"Sliding ttl misconfigured as negative in appsettings; a computed duration (end - now) that is negative because now is past end; arithmetic error producing -duration.","solutions":["Pass a non-negative sliding TimeSpan (positive recommended; zero is allowed but useless).","Clamp configured sliding ttl to TimeSpan.Zero or a positive minimum before constructing SlidingTtl.","Default to a sane positive value when config is missing/invalid."],"exampleFix":"// before\nnew SlidingTtl(TimeSpan.FromMinutes(-5));\n// after\nnew SlidingTtl(TimeSpan.FromMinutes(5));","handlingStrategy":"validation","validationCode":"if (slidingTtl < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(slidingTtl), \"sliding ttl must be non-negative.\");\nvar strategy = new SlidingTtl(slidingTtl);","typeGuard":"static bool IsTtlValid(TimeSpan ttl) => ttl >= TimeSpan.Zero;","tryCatchPattern":null,"preventionTips":["Clamp configured sliding ttl to a non-negative value, ideally a positive minimum.","Validate appsettings sliding-ttl with a fallback default.","Guard computed durations (end - now) against going negative."],"tags":["polly","caching","legacy-v7","argument-out-of-range","ttl","sliding","configuration"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}