feat: Add caching layer and batch operations for improved performance

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>
This commit is contained in:
Claude
2025-10-23 14:46:58 +00:00
parent 32853476f0
commit dc0d72c896
7 changed files with 1048 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from .auth import APIKeyAuth, AuthHandler
from .cache import BaseCache
from .endpoints import AssetsEndpoint, GroupsEndpoint, PagesEndpoint, UsersEndpoint
from .exceptions import (
APIError,
@@ -39,6 +40,7 @@ class WikiJSClient:
timeout: Request timeout in seconds (default: 30)
verify_ssl: Whether to verify SSL certificates (default: True)
user_agent: Custom User-Agent header
cache: Optional cache instance for caching API responses
Example:
Basic usage with API key:
@@ -47,10 +49,19 @@ class WikiJSClient:
>>> pages = client.pages.list()
>>> page = client.pages.get(123)
With caching enabled:
>>> from wikijs.cache import MemoryCache
>>> cache = MemoryCache(ttl=300)
>>> client = WikiJSClient('https://wiki.example.com', auth='your-api-key', cache=cache)
>>> page = client.pages.get(123) # Fetches from API
>>> page = client.pages.get(123) # Returns from cache
Attributes:
base_url: The normalized base URL
timeout: Request timeout setting
verify_ssl: SSL verification setting
cache: Optional cache instance
"""
def __init__(
@@ -60,6 +71,7 @@ class WikiJSClient:
timeout: int = 30,
verify_ssl: bool = True,
user_agent: Optional[str] = None,
cache: Optional[BaseCache] = None,
):
# Instance variable declarations for mypy
self._auth_handler: AuthHandler
@@ -85,6 +97,9 @@ class WikiJSClient:
self.verify_ssl = verify_ssl
self.user_agent = user_agent or f"wikijs-python-sdk/{__version__}"
# Cache configuration
self.cache = cache
# Initialize HTTP session
self._session = self._create_session()