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

121
wikijs/cache/base.py vendored Normal file
View File

@@ -0,0 +1,121 @@
"""Base cache interface for wikijs-python-sdk."""
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Optional
@dataclass
class CacheKey:
"""Cache key structure for Wiki.js resources.
Attributes:
resource_type: Type of resource (e.g., 'page', 'user', 'group')
identifier: Unique identifier (ID, path, etc.)
operation: Operation type (e.g., 'get', 'list')
params: Additional parameters as string (e.g., 'locale=en&tags=api')
"""
resource_type: str
identifier: str
operation: str = "get"
params: Optional[str] = None
def to_string(self) -> str:
"""Convert cache key to string format.
Returns:
String representation suitable for cache storage
Example:
>>> key = CacheKey('page', '123', 'get')
>>> key.to_string()
'page:123:get'
"""
parts = [self.resource_type, str(self.identifier), self.operation]
if self.params:
parts.append(self.params)
return ":".join(parts)
class BaseCache(ABC):
"""Abstract base class for cache implementations.
All cache backends must implement this interface to be compatible
with the WikiJS SDK.
Args:
ttl: Time-to-live in seconds (default: 300 = 5 minutes)
max_size: Maximum number of items to cache (default: 1000)
"""
def __init__(self, ttl: int = 300, max_size: int = 1000):
"""Initialize cache with TTL and size limits.
Args:
ttl: Time-to-live in seconds for cached items
max_size: Maximum number of items to store
"""
self.ttl = ttl
self.max_size = max_size
@abstractmethod
def get(self, key: CacheKey) -> Optional[Any]:
"""Retrieve value from cache.
Args:
key: Cache key to retrieve
Returns:
Cached value if found and not expired, None otherwise
"""
pass
@abstractmethod
def set(self, key: CacheKey, value: Any) -> None:
"""Store value in cache.
Args:
key: Cache key to store under
value: Value to cache
"""
pass
@abstractmethod
def delete(self, key: CacheKey) -> None:
"""Remove value from cache.
Args:
key: Cache key to remove
"""
pass
@abstractmethod
def clear(self) -> None:
"""Clear all cached values."""
pass
@abstractmethod
def invalidate_resource(self, resource_type: str, identifier: Optional[str] = None) -> None:
"""Invalidate all cache entries for a resource.
Args:
resource_type: Type of resource to invalidate (e.g., 'page', 'user')
identifier: Specific identifier to invalidate (None = all of that type)
Example:
>>> cache.invalidate_resource('page', '123') # Invalidate page 123
>>> cache.invalidate_resource('page') # Invalidate all pages
"""
pass
def get_stats(self) -> dict:
"""Get cache statistics.
Returns:
Dictionary with cache statistics (hits, misses, size, etc.)
"""
return {
"ttl": self.ttl,
"max_size": self.max_size,
}