Skip to main content

Overview

The Engine class is the heart of Lyger’s performance optimization layer. It provides a singleton interface to Rust-powered FFI functions for heavy computations, caching, database operations, and the Always-Alive server.
The Engine automatically falls back to PHP implementations when FFI is not available, ensuring compatibility across all environments.

getInstance

Get the singleton instance of the Engine.

Returns

Engine
The singleton Engine instance

Example

Server Management

startServer

Start the Always-Alive server with Rust-powered HTTP handling.
callable
required
Callback function that handles routing logic
int
default:"8000"
Port number to run the server on

Example

stopServer

Stop the running server and clean up resources.

Example

Basic Functions

helloWorld

Get a hello world message from the Rust layer.

Returns

string
Hello message from Lyger

Example

heavyComputation

Perform CPU-intensive mathematical computations with Rust optimization.
int
default:"1000000"
Number of computational iterations to perform

Returns

float
Computed result from the heavy computation

Example

systemInfo

Get system and framework information as JSON.

Returns

string
JSON string containing framework version, mode, status, and PHP version

Example

Cache Operations

cacheSet

Store a value in the Rust-powered in-memory cache.
string
required
Cache key identifier
string
required
Value to store in cache

Example

cacheGet

Retrieve a value from the cache.
string
required
Cache key to retrieve

Returns

string
Cached value, or empty string if not found

Example

cacheDelete

Remove a specific key from the cache.
string
required
Cache key to delete

Example

cacheClear

Clear all entries from the cache.

Example

cacheSize

Get the number of entries currently in the cache.

Returns

int
Number of cache entries

Example

Zero-Copy Database Operations

dbQuery

Execute a database query and return a pointer to the result set.
string
required
Database connection string (e.g., “postgres://user:pass@localhost/db”)
string
required
SQL query to execute

Returns

int
Memory pointer to the result set (0 if query fails)

Example

jsonifyResult

Convert a result set pointer to JSON format.
int
required
Result set pointer from dbQuery

Returns

string
JSON string containing query results

Example

freeResult

Free the memory allocated for a result set.
int
required
Result set pointer to free
Always call freeResult() after you’re done with a result set to prevent memory leaks.

Example

dbQueryJson

Convenience method that executes a query and returns JSON directly.
string
required
Database connection string
string
required
SQL query to execute

Returns

string
JSON string containing query results
This method automatically handles memory cleanup, making it the recommended way to perform database queries.

Example

Complete Example