Implement Phase 3 improvements: intelligent caching and batch operations
to significantly enhance SDK performance and usability.
**1. Caching Layer Implementation**
Added complete caching infrastructure with LRU eviction and TTL support:
- `wikijs/cache/base.py`: Abstract BaseCache interface with CacheKey structure
- `wikijs/cache/memory.py`: MemoryCache implementation with:
* LRU (Least Recently Used) eviction policy
* Configurable TTL (time-to-live) expiration
* Cache statistics (hits, misses, hit rate)
* Resource-specific invalidation
* Automatic cleanup of expired entries
**Cache Integration:**
- Modified `WikiJSClient` to accept optional `cache` parameter
- Integrated caching into `PagesEndpoint.get()`:
* Check cache before API request
* Store successful responses in cache
* Invalidate cache on write operations (update, delete)
**2. Batch Operations**
Added efficient batch methods to Pages API:
- `create_many(pages_data)`: Batch create multiple pages
- `update_many(updates)`: Batch update pages with partial success handling
- `delete_many(page_ids)`: Batch delete with detailed error reporting
All batch methods include:
- Partial success support (continue on errors)
- Detailed error tracking with indices
- Comprehensive error messages
**3. Comprehensive Testing**
Added 27 new tests (all passing):
- `tests/test_cache.py`: 17 tests for caching (99% coverage)
* CacheKey string generation
* TTL expiration
* LRU eviction policy
* Cache invalidation (specific & all resources)
* Statistics tracking
- `tests/endpoints/test_pages_batch.py`: 10 tests for batch operations
* Successful batch creates/updates/deletes
* Partial failure handling
* Empty list edge cases
* Validation error handling
**Performance Benefits:**
- Caching reduces API calls for frequently accessed pages
- Batch operations reduce network overhead for bulk actions
- Configurable cache size and TTL for optimization
**Example Usage:**
```python
from wikijs import WikiJSClient
from wikijs.cache import MemoryCache
# Enable caching
cache = MemoryCache(ttl=300, max_size=1000)
client = WikiJSClient('https://wiki.example.com', auth='key', cache=cache)
# Cached GET requests
page = client.pages.get(123) # Fetches from API
page = client.pages.get(123) # Returns from cache
# Batch operations
pages = client.pages.create_many([
PageCreate(title="Page 1", path="page-1", content="Content 1"),
PageCreate(title="Page 2", path="page-2", content="Content 2"),
])
updates = client.pages.update_many([
{"id": 1, "content": "Updated content"},
{"id": 2, "is_published": False},
])
result = client.pages.delete_many([1, 2, 3])
print(f"Deleted {result['successful']} pages")
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
25 lines
776 B
Python
25 lines
776 B
Python
"""Caching module for wikijs-python-sdk.
|
|
|
|
This module provides intelligent caching for frequently accessed Wiki.js resources
|
|
like pages, users, and groups. It supports multiple cache backends and TTL-based
|
|
expiration.
|
|
|
|
Example:
|
|
>>> from wikijs import WikiJSClient
|
|
>>> from wikijs.cache import MemoryCache
|
|
>>>
|
|
>>> cache = MemoryCache(ttl=300) # 5 minute TTL
|
|
>>> client = WikiJSClient('https://wiki.example.com', auth='api-key', cache=cache)
|
|
>>>
|
|
>>> # First call hits the API
|
|
>>> page = client.pages.get(123)
|
|
>>>
|
|
>>> # Second call returns cached result
|
|
>>> page = client.pages.get(123) # Instant response
|
|
"""
|
|
|
|
from .base import BaseCache, CacheKey
|
|
from .memory import MemoryCache
|
|
|
|
__all__ = ["BaseCache", "CacheKey", "MemoryCache"]
|