-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add best-effort TTL support to the API (#40)
* Updated the docs. * Added TTL and request validation to the PUT contract classes. * Added max_ttl_seconds to the config request limits. * Added Request-based TTLs to the backends. * Moved TTL-limiting into a backend decorator. * Undid some unnecessary changes. * Made aerospike use the configurable default TTL.
- Loading branch information
Showing
19 changed files
with
133 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package decorators | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prebid/prebid-cache/backends" | ||
) | ||
|
||
// LimitTTLs wraps the delegate and makes sure that it never gets TTLs which exceed the max. | ||
func LimitTTLs(delegate backends.Backend, maxTTLSeconds int) backends.Backend { | ||
return ttlLimited{ | ||
Backend: delegate, | ||
maxTTLSeconds: maxTTLSeconds, | ||
} | ||
} | ||
|
||
type ttlLimited struct { | ||
backends.Backend | ||
maxTTLSeconds int | ||
} | ||
|
||
func (l ttlLimited) Put(ctx context.Context, key string, value string, ttlSeconds int) error { | ||
if l.maxTTLSeconds > ttlSeconds { | ||
return l.Backend.Put(ctx, key, value, ttlSeconds) | ||
} | ||
return l.Backend.Put(ctx, key, value, l.maxTTLSeconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package decorators_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-cache/backends/decorators" | ||
) | ||
|
||
func TestExcessiveTTL(t *testing.T) { | ||
delegate := &ttlCapturer{} | ||
wrapped := decorators.LimitTTLs(delegate, 100) | ||
wrapped.Put(context.Background(), "foo", "bar", 200) | ||
if delegate.lastTTL != 100 { | ||
t.Errorf("lastTTL should be %d. Got %d", 100, delegate.lastTTL) | ||
} | ||
} | ||
|
||
func TestSafeTTL(t *testing.T) { | ||
delegate := &ttlCapturer{} | ||
wrapped := decorators.LimitTTLs(delegate, 100) | ||
wrapped.Put(context.Background(), "foo", "bar", 50) | ||
if delegate.lastTTL != 50 { | ||
t.Errorf("lastTTL should be %d. Got %d", 50, delegate.lastTTL) | ||
} | ||
} | ||
|
||
type ttlCapturer struct { | ||
lastTTL int | ||
} | ||
|
||
func (c *ttlCapturer) Put(ctx context.Context, key string, value string, ttlSeconds int) error { | ||
c.lastTTL = ttlSeconds | ||
return nil | ||
} | ||
|
||
func (c *ttlCapturer) Get(ctx context.Context, key string) (string, error) { | ||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.