Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cmatosbc authored Nov 4, 2024
1 parent 160c336 commit 7780a9a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ This function is designed to cache the results of specific WordPress queries. It
* Expiration: The $expires parameter can be a DateTime object or an integer representing seconds.
* Serialization: Results are serialized before caching and deserialized when retrieved.

### Example

```php
// Cache the results of a WP_Query for 1 hour
$cachedQuery = withCachedQuery($cache, 3600);

// Use the cached query to fetch recent posts
$recentPosts = $cachedQuery([
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
]);
```

## ```withCache()``` Function

This function is a more generic caching mechanism that can be used to cache the results of any callable function or method. It takes a cache interface and an expiration time as input. It returns a closure that, when called with a callable and its arguments, will:
Expand All @@ -34,6 +49,19 @@ This function is a more generic caching mechanism that can be used to cache the
* Flexible Caching: The $process parameter can be a closure, a string (function name), or an array (method call).
* Cache Key Generation: Cache keys are generated based on the callable's reflection and arguments, ensuring unique keys for different calls.

### Example

```php
// Cache the result of a custom function for 24 hours
$cachedFunction = withCache($cache, 86400);

// Use the cached function to fetch a user's profile data
$userProfile = $cachedFunction(function($userId) {
// Fetch user profile data from a database or API
return getUserProfile($userId);
}, 123); // Pass the user ID as an argument
```

## Use Cases:

* Performance Optimization: Reduce database queries and improve page load times by caching frequently used data.
Expand Down

0 comments on commit 7780a9a

Please sign in to comment.