Skip to content

Commit 62c3c89

Browse files
TexasCodingclaude
andcommitted
feat: Add comprehensive Caching System for performance optimization
- Create cache/ module with LRU memory cache and optional Redis support - Implement CacheManager with configurable TTL per data type - Add cache invalidation by pattern and prefix clearing - Support for dataclass serialization and decorator-based caching - Implement cache statistics tracking (hit/miss rates) - Add 40 comprehensive tests (29 unit, 11 integration) - Configure sensible TTLs: 1 day for market hours, 1 hour for assets, etc. Features: - LRU in-memory cache with size limits - Optional Redis backend with automatic fallback - Per-data-type TTL configuration - Cache key generation with hash for long keys - Thread-safe concurrent access - Decorator for easy function result caching - Pattern-based cache invalidation - Detailed statistics tracking This completes Phase 3 of the v3.0.0 development plan. All Performance & Quality features are now implemented. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1cafdcf commit 62c3c89

File tree

6 files changed

+1234
-11
lines changed

6 files changed

+1234
-11
lines changed

DEVELOPMENT_PLAN.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,22 @@ main
230230
- Clear error messages for feed issues
231231
- Configuration for preferred feeds
232232

233-
#### 3.3 Caching System
233+
#### 3.3 Caching System
234234
**Branch**: `feature/caching-system`
235235
**Priority**: 🟢 Medium
236236
**Estimated Time**: 3 days
237+
**Actual Time**: < 1 day
238+
**Completed**: 2025-01-16
237239

238240
**Tasks**:
239-
- [ ] Create `cache/` module structure
240-
- [ ] Implement `CacheManager` class
241-
- [ ] Add LRU in-memory cache
242-
- [ ] Add optional Redis support
243-
- [ ] Implement cache invalidation logic
244-
- [ ] Configure TTL per data type
245-
- [ ] Add comprehensive tests (10+ test cases)
246-
- [ ] Update documentation
241+
- [x] Create `cache/` module structure
242+
- [x] Implement `CacheManager` class
243+
- [x] Add LRU in-memory cache
244+
- [x] Add optional Redis support
245+
- [x] Implement cache invalidation logic
246+
- [x] Configure TTL per data type
247+
- [x] Add comprehensive tests (40 test cases: 29 unit, 11 integration)
248+
- [x] Update documentation
247249

248250
**Acceptance Criteria**:
249251
- Configurable caching per data type
@@ -298,13 +300,13 @@ main
298300

299301
## 📈 Progress Tracking
300302

301-
### Overall Progress: 🟦 67% Complete
303+
### Overall Progress: 🟦 75% Complete
302304

303305
| Phase | Status | Progress | Estimated Completion |
304306
|-------|--------|----------|---------------------|
305307
| Phase 1: Critical Features | ✅ Complete | 100% | Week 1 |
306308
| Phase 2: Important Enhancements | ✅ Complete | 100% | Week 2 |
307-
| Phase 3: Performance & Quality | 🟦 In Progress | 67% | Week 7 |
309+
| Phase 3: Performance & Quality | ✅ Complete | 100% | Week 2 |
308310
| Phase 4: Advanced Features | ⬜ Not Started | 0% | Week 10 |
309311

310312
### Feature Status Legend
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Cache module for py-alpaca-api.
2+
3+
This module provides caching functionality to improve performance and reduce API calls.
4+
"""
5+
6+
from .cache_config import CacheConfig, CacheType
7+
from .cache_manager import CacheManager
8+
9+
__all__ = ["CacheManager", "CacheConfig", "CacheType"]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Cache configuration for py-alpaca-api."""
2+
3+
from __future__ import annotations
4+
5+
from dataclasses import dataclass, field
6+
from enum import Enum
7+
8+
9+
class CacheType(Enum):
10+
"""Types of cache backends supported."""
11+
12+
MEMORY = "memory"
13+
REDIS = "redis"
14+
DISABLED = "disabled"
15+
16+
17+
@dataclass
18+
class CacheConfig:
19+
"""Configuration for cache system.
20+
21+
Attributes:
22+
cache_type: Type of cache backend to use
23+
max_size: Maximum number of items in memory cache
24+
default_ttl: Default time-to-live in seconds
25+
data_ttls: TTL overrides per data type
26+
redis_host: Redis host (if using Redis)
27+
redis_port: Redis port (if using Redis)
28+
redis_db: Redis database number (if using Redis)
29+
redis_password: Redis password (if using Redis)
30+
enabled: Whether caching is enabled
31+
"""
32+
33+
cache_type: CacheType = CacheType.MEMORY
34+
max_size: int = 1000
35+
default_ttl: int = 300 # 5 minutes default
36+
data_ttls: dict[str, int] = field(
37+
default_factory=lambda: {
38+
"market_hours": 86400, # 1 day
39+
"calendar": 86400, # 1 day
40+
"assets": 3600, # 1 hour
41+
"account": 60, # 1 minute
42+
"positions": 10, # 10 seconds
43+
"orders": 5, # 5 seconds
44+
"quotes": 1, # 1 second
45+
"bars": 60, # 1 minute
46+
"trades": 60, # 1 minute
47+
"news": 300, # 5 minutes
48+
"watchlists": 300, # 5 minutes
49+
"snapshots": 1, # 1 second
50+
"metadata": 86400, # 1 day (condition codes, exchanges)
51+
}
52+
)
53+
redis_host: str = "localhost"
54+
redis_port: int = 6379
55+
redis_db: int = 0
56+
redis_password: str | None = None
57+
enabled: bool = True
58+
59+
def get_ttl(self, data_type: str) -> int:
60+
"""Get TTL for a specific data type.
61+
62+
Args:
63+
data_type: Type of data to get TTL for
64+
65+
Returns:
66+
TTL in seconds
67+
"""
68+
return self.data_ttls.get(data_type, self.default_ttl)

0 commit comments

Comments
 (0)