Overview
ZhiChai Forum is a modern forum system developed in native PHP (no framework), fully migrated from an earlier Go version. It uses a distributed design with Redis + SQLite dual storage, asynchronous processing, caching, and a complete user permission system.
Key points
- Native PHP backend: no PHP framework; MVC structure (
src/Models/,src/Controllers/,views/) plus service-oriented components (UserService,TopicService,ReplyService,PermissionService,CacheRefillService,EmojiService). - Dependency injection via the Dice DI container: constructor auto-injection, singleton services, aliases.
- Dual storage: Redis serves sessions, topic lists, replies, emoji counters, and queues; SQLite provides durable tables (
users,topics,replies,user_emojis,passkeys). - Async SQLite writes: writes go to Redis + an async queue and return immediately; a background worker (
process_sqlite_queue.php) batch-writes SQLite (10 tasks per batch) and syncs cache. Failed tasks retry up to 3 times with exponential backoff (5s → 10s → 20s), then go to a dead-letter queue. - Claimed performance gains (async vs. sync): user registration ~200ms → ~20ms, new topic ~150ms → ~15ms, reply ~100ms → ~10ms (~90% improvement).
- Three-tier RBAC (user / admin / super admin) covering content editing/deletion, topic close/hide, reply hiding, user freeze, and role changes, with protection against demoting the last super admin.
- Frontend: Bootstrap 5.3.3, HTMX 1.9.12, Marked.js 9.1.2, DOMPurify 3.0.5; Markdown editing with live preview, nested/quoted replies, 7 emoji reactions, responsive design, dark theme, ARIA support, PWA features.
- Auth: username+password (bcrypt, cost 12) plus WebAuthn (fingerprint/Face ID) passkeys stored in a
passkeystable; secure session management. - Caching: multi-layer (PHP memory → Redis 1h TTL → SQLite), with proactive update, lazy invalidation, and pre-warming; admin cache status/warm/clear UI.
- Security: parameterized SQL, output escaping + DOMPurify for XSS, CSRF tokens, audit logs.
- Health check at
/health, async queue status at/debug/async. - Queue CLI:
php process_sqlite_queue.php status|dead-letter|daemon. - Stack: PHP 8.0+, SQLite 3.x, Redis 6.x, Composer (Dice, WebAuthn), Nginx/Apache + PHP-FPM.
- Scale: ~25,000 lines of PHP, 156 files, 25 docs, 20+ test scripts, 30+ admin scripts.
- Deployment: single-machine for 1k–10k user communities; supports Docker, Redis clustering, and cloud/microservice setups.