Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Created March 17, 2026 18:05
Show Gist options
  • Select an option

  • Save aj07mm/2a99f9303d8f7e5c8694f5e9a6f17ab5 to your computer and use it in GitHub Desktop.

Select an option

Save aj07mm/2a99f9303d8f7e5c8694f5e9a6f17ab5 to your computer and use it in GitHub Desktop.
"""
You need to design and implement a **Rate-Limited Cache System** that serves as
a middleware layer for expensive API calls. The system should provide caching
functionality while preventing abuse through rate limiting and collecting
analytics.
### Core Functionality
1. **Cache Operations**: Implement `get(key)` and `put(key, value, ttl)`
operations
2. **Rate Limiting**: Limit requests per user (configurable rate limit)
3. **TTL Support**: Cache entries should expire after their time-to-live
4. **Analytics**: Track cache hits, misses, and rate limit violations
### Technical Constraints
- Cache should have a maximum capacity with LRU eviction
least recently used
- Rate limiting should use a sliding window approach
- Include proper error handling and logging
"""
from typing import Optional, Dict, Any
from threading import Lock
from datetime import datetime, timedelta
import time
import logging
rate_limit = 2
lru_limit = 5000
class RateLimitedCache:
storage = {}
def __init__(self, max_capacity: int = 500, default_rate_limit: int = 60):
"""
Initialize the rate-limited cache system.
Args:
max_capacity: Maximum number of items in cache
default_rate_limit: Default requests per minute per user
"""
pass
def get(self, key: str, user_id: str) -> Optional[Any]:
"""
Retrieve a value from the cache with rate limiting.
Args:
key: The cache key
user_id: Identifier for rate limiting
Returns:
The cached value if found and rate limit not exceeded, None otherwise
Raises:
RateLimitExceeded: If user has exceeded their rate limit
"""
"""
if cache_data["user_id"] <= rate_limit:
else:
return
"""
cache_data = self.storage.get(key)
ttl = timedelta(seconds=cache_data["ttl"])
if cache_data["created_at"] + ttl < datetime.now():
return None
return cache_data
def put(self, key: str, value: Any, ttl: int, user_id: str) -> bool:
"""
Store a value in the cache with rate limiting.
Args:
key: The cache key
value: The value to store
ttl: Time to live in seconds
user_id: Identifier for rate limiting
Returns:
True if stored successfully, False if rate limited
"""
# remove the oldest records when the cache overflows
if len(self.storage.keys()) < lru_limit:
self.storage[key] = {"value": value, "user_id": user_id, "created_at": datetime.now(), "ttl": ttl}
return True
else:
# clean
# remove the oldest record
self.storage.
# add the key/value
self.storage[key] = {"value": value, "user_id": user_id, "created_at": datetime.now(), "ttl": ttl}
def get_analytics(self) -> Dict[str, Any]:
"""
Get system analytics.
Returns:
Dictionary with cache hits, misses, rate limit violations, etc.
"""
pass
def set_user_rate_limit(self, user_id: str, rate_limit: int):
"""Set custom rate limit for a specific user."""
pass
def test_put():
value = {}
ttl = 50
user_id = 1
cache_storage = RateLimitedCache()
assert cache_storage.put("foo-1-key", value, ttl, user_id) == True
def test_get_retrieval():
value = {}
ttl = 50
user_id = 1
cache_storage = RateLimitedCache()
cache_storage.put("foo-1-key", value, ttl, user_id)
cache_data = cache_storage.get("foo-1-key", user_id)
assert cache_data["value"] == value
def test_get_retrieval_with_ttl_passing():
value = {}
ttl = 50
user_id = 1
cache_storage = RateLimitedCache()
cache_storage.put("foo-1-key", value, ttl, user_id) # now
time.sleep(4)
cache_data = cache_storage.get("foo-1-key", user_id)
assert cache_data == None
#test_put()
#test_get_retrieval()
test_get_retrieval_with_ttl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment