A behavioral spam detection system with pluggable cache service and sliding window rate limiter using Redis zsets.
- Behavioral Spam Detection: Analyzes patterns in user behavior to detect spam
- Pluggable Cache Service: Interface-based design allowing different cache implementations
- Sliding Window Rate Limiter: Uses Redis zsets with timestamp scores and UUID members
- Redis Integration: Optimized for Redis zset operations
spam_detector.py- Core behavioral spam detection logiccache/- Cache service interfaces and implementationsbase.py- Abstract cache interfaceredis_cache.py- Redis implementationmemory_cache.py- In-memory implementation for testing
rate_limiter.py- Sliding window rate limiter using Redis zsetsmodels.py- Data models and typesconfig.py- Configuration management
pip install -r requirements.txtSet environment variables:
REDIS_URL=redis://localhost:6379/0
SPAM_DETECTION_THRESHOLD=0.7
RATE_LIMIT_WINDOW_SECONDS=3600
RATE_LIMIT_MAX_REQUESTS=100from spam_handler import SpamDetector, RateLimiter
from spam_handler.cache import RedisCache
# Initialize components
cache = RedisCache()
detector = SpamDetector(cache)
rate_limiter = RateLimiter(cache)
# Check for spam
is_spam = detector.is_spam(user_id, content, metadata)
# Check rate limit
is_allowed = rate_limiter.is_allowed(user_id, action_type)python -m pytest tests/