{"id":29651306,"url":"https://github.com/arraypress/wp-transient-utils","last_synced_at":"2025-07-22T05:06:34.742Z","repository":{"id":302293184,"uuid":"1011462741","full_name":"arraypress/wp-transient-utils","owner":"arraypress","description":"A lean WordPress library for working with transients and caching.","archived":false,"fork":false,"pushed_at":"2025-07-01T14:41:50.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-01T15:42:46.645Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arraypress.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-30T21:24:02.000Z","updated_at":"2025-07-01T14:41:53.000Z","dependencies_parsed_at":"2025-07-01T15:55:42.809Z","dependency_job_id":null,"html_url":"https://github.com/arraypress/wp-transient-utils","commit_stats":null,"previous_names":["arraypress/wp-transient-utils"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arraypress/wp-transient-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-transient-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-transient-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-transient-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-transient-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arraypress","download_url":"https://codeload.github.com/arraypress/wp-transient-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-transient-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266430665,"owners_count":23927169,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-22T05:06:34.203Z","updated_at":"2025-07-22T05:06:34.718Z","avatar_url":"https://github.com/arraypress.png","language":"PHP","readme":"# WordPress Transients Utilities\n\nA lightweight WordPress library for working with transients and caching. Provides clean APIs for transient operations, bulk management, pattern-based deletion, and high-level caching patterns with WordPress-style global functions.\n\n## Features\n\n* 🎯 **Clean API**: WordPress-style snake_case methods and global functions\n* 🚀 **Core Operations**: Type casting, numeric operations, essential utilities\n* 📊 **Bulk Management**: Process multiple transients efficiently\n* 🔍 **Pattern Matching**: Delete by prefix and custom patterns\n* 📈 **Essential Tools**: Size calculation, type detection, boolean toggles\n* 💾 **Remember Pattern**: High-level caching with automatic computation\n* 🌍 **Global Functions**: WordPress-style helper functions for common operations\n\n## Installation\n\n```bash\ncomposer require arraypress/wp-transient-utils\n```\n\n## Quick Start (Global Functions)\n\nThe fastest way to get started is with the WordPress-style global functions:\n\n```php\n// Remember pattern - get from cache or compute\n$expensive_data = wp_cache_remember( 'user_analytics_123', function () {\n\treturn perform_complex_analytics_query( 123 );\n}, HOUR_IN_SECONDS );\n\n// Auto-generated cache keys\n$user_stats = wp_cache_remember_by( 'user_stats', function () use ( $user_id ) {\n\treturn calculate_user_statistics( $user_id );\n}, HOUR_IN_SECONDS, $user_id );\n\n// Increment counters\n$page_views = wp_transient_increment( 'page_views_' . $post_id );\n\n// Toggle feature flags\n$maintenance_mode = wp_transient_toggle( 'maintenance_mode', HOUR_IN_SECONDS );\n```\n\n## Class-Based API\n\nFor more advanced usage, use the class-based API:\n\n### Single Transient Operations\n\n```php\nuse ArrayPress\\TransientsUtils\\Transient;\nuse ArrayPress\\TransientsUtils\\Cache;\n\n// Basic operations\n$exists = Transient::exists( 'my_transient' );\n$value  = Transient::get( 'my_transient' );\n$value  = Transient::get_with_default( 'my_transient', 'default' );\n\n// Type casting\n$int_value   = Transient::get_cast( 'my_transient', 'int', 0 );\n$array_value = Transient::get_cast( 'my_transient', 'array', [] );\n$bool_value  = Transient::get_cast( 'my_transient', 'bool', false );\n\n// Set/delete\nTransient::set( 'my_transient', 'value', 3600 );\nTransient::delete( 'my_transient' );\n\n// Numeric operations\n$new_value = Transient::increment_value( 'counter', 1, 3600 );\n$new_value = Transient::decrement_value( 'counter', 1, 3600 );\n\n// Utility methods\n$type    = Transient::get_type( 'my_transient' );       // 'string', 'array', etc.\n$size    = Transient::get_size( 'my_transient' );       // Size in bytes\n$toggled = Transient::toggle( 'boolean_flag', 3600 );   // Toggle boolean value\n\n// Validation helpers\n$is_array = Transient::is_type( 'my_transient', 'array' );  // Check if specific type\n$is_large = Transient::is_large( 'my_transient' );          // Check if \u003e 1MB\n$is_large = Transient::is_large( 'my_transient', 512000 );  // Check if \u003e 500KB\n```\n\n### High-Level Caching (Remember Pattern)\n\n```php\n// The remember pattern - get from cache or compute and store\n$expensive_data = Cache::remember( 'user_analytics_123', function () {\n\t// This expensive operation only runs on cache miss\n\treturn perform_complex_analytics_query( 123 );\n}, HOUR_IN_SECONDS );\n\n// Remember with automatic key generation\n$user_stats = Cache::remember_by( 'user_stats', function () use ( $user_id ) {\n\treturn calculate_user_statistics( $user_id );\n}, HOUR_IN_SECONDS, $user_id );\n\n// Complex key generation with multiple parameters\n$filtered_posts = Cache::remember_by( 'filtered_posts', function () use ( $category, $tag, $limit ) {\n\treturn get_complex_filtered_posts( $category, $tag, $limit );\n}, 30 * MINUTE_IN_SECONDS, $category, $tag, $limit );\n```\n\n### Multiple Transient Operations\n\n```php\nuse ArrayPress\\TransientsUtils\\Transients;\n\n// Bulk operations\n$existing      = Transients::exists( [ 'trans1', 'trans2', 'trans3' ] );\n$values        = Transients::get( [ 'trans1', 'trans2' ] );\n$set_results   = Transients::set( [ 'key1' =\u003e 'value1', 'key2' =\u003e 'value2' ], 3600 );\n$deleted_count = Transients::delete( [ 'trans1', 'trans2' ] );\n\n// Discovery\n$all_transients = Transients::get_all();                    // Get all transients\n$all_names      = Transients::get_all( false );             // Get names only\n$prefixed       = Transients::get_by_prefix( 'my_plugin_' ); // Get by prefix\n\n// Pattern-based deletion\n$deleted = Transients::delete_by_prefix( 'my_plugin_' );           // Delete by prefix\n$deleted = Transients::delete_by_pattern( 'cache_', 'prefix' );    // Custom patterns\n$deleted = Transients::delete_by_pattern( '_temp', 'suffix' );     // Suffix pattern\n$deleted = Transients::delete_by_pattern( 'user', 'substring' );   // Substring pattern\n```\n\n## Practical Examples\n\n### Real-World Caching with Global Functions\n```php\n// Cache expensive API calls\n$weather_data = wp_cache_remember_by( 'weather_api', function () use ( $city ) {\n\treturn wp_remote_get( \"https://api.weather.com/current/{$city}\" );\n}, 15 * MINUTE_IN_SECONDS, $city );\n\n// Cache database queries\n$popular_posts = wp_cache_remember( 'popular_posts_today', function () {\n\tglobal $wpdb;\n\n\treturn $wpdb-\u003eget_results( \"\n        SELECT p.*, COUNT(v.post_id) as view_count \n        FROM wp_posts p \n        LEFT JOIN wp_post_views v ON p.ID = v.post_id \n        WHERE p.post_status = 'publish' \n        GROUP BY p.ID \n        ORDER BY view_count DESC \n        LIMIT 10\n    \" );\n}, HOUR_IN_SECONDS );\n\n// Track page views\n$new_views = wp_transient_increment( 'page_views_' . get_the_ID() );\n\n// Feature flags\nif ( wp_transient_toggle( 'beta_features_enabled' ) ) {\n\t// Show beta features\n}\n```\n\n### Advanced Remember Pattern Usage\n```php\n// Time-based cache keys\n$daily_stats = Cache::remember( 'daily_stats_' . date( 'Y-m-d' ), function () {\n\treturn generate_daily_statistics();\n}, DAY_IN_SECONDS );\n\n// Conditional caching based on user type\n$cache_key      = $user-\u003eis_premium() ? 'premium_dashboard_' . $user_id : 'basic_dashboard_' . $user_id;\n$dashboard_data = Cache::remember( $cache_key, function () use ( $user ) {\n\treturn generate_dashboard_data( $user );\n}, HOUR_IN_SECONDS );\n\n// Multi-parameter auto-generated keys\n$search_results = Cache::remember_by( 'search_results', function () use ( $query, $filters, $page ) {\n\treturn perform_complex_search( $query, $filters, $page );\n}, 30 * MINUTE_IN_SECONDS, $query, $filters, $page );\n```\n\n### Numeric Counters and Analytics\n```php\n// Using global functions (recommended)\nwp_transient_increment( 'api_requests_' . $user_id );\nwp_transient_increment( 'downloads_' . $file_id, 1, WEEK_IN_SECONDS );\n\n// Using class methods (for advanced control)\n$new_count = Transient::increment_value( 'page_views', 5, DAY_IN_SECONDS );\n$new_count = Transient::decrement_value( 'credits_remaining', 1, MONTH_IN_SECONDS );\n```\n\n### Type-Safe Data Retrieval\n```php\n// Ensure consistent data types\n$user_id    = Transient::get_cast( 'current_user', 'int', 0 );\n$settings   = Transient::get_cast( 'site_config', 'array', [] );\n$is_enabled = Transient::get_cast( 'feature_flag', 'bool', false );\n\n// Validate data types with helper methods\nif ( Transient::is_type( 'user_preferences', 'array' ) ) {\n\t$prefs = Transient::get( 'user_preferences' );\n\t// Process array safely\n}\n\n// Size validation for performance\nif ( ! Transient::is_large( 'api_response' ) ) {\n\t// Safe to process - not too large\n\t$data = Transient::get( 'api_response' );\n}\n```\n\n### Cache Management and Cleanup\n```php\n// Plugin cleanup - remove all plugin caches\n$deleted = Transients::delete_by_prefix( 'my_plugin_' );\n\n// Pattern-based cleanup\n$deleted = Transients::delete_by_pattern( '_temp', 'suffix' );\n$deleted = Transients::delete_by_pattern( 'user_', 'substring' );\n\n// Global functions create consistent prefixes for easy cleanup\nwp_cache_remember_by( 'user_data', $callback, $expiration, $user_id );\nwp_cache_remember_by( 'user_stats', $callback, $expiration, $user_id );\n\n// Later, clean all user-related caches\nTransients::delete_by_prefix( 'user_data_' );\nTransients::delete_by_prefix( 'user_stats_' );\n```\n\n## API Reference\n\n### Global Helper Functions\n\n**Remember Pattern:**\n- `wp_cache_remember( $key, $callback, $expiration )` - Get from cache or compute\n- `wp_cache_remember_by( $prefix, $callback, $expiration, ...$args )` - Remember with auto-generated keys\n\n**Common Operations:**\n- `wp_transient_increment( $key, $amount, $expiration )` - Increment counter\n- `wp_transient_toggle( $key, $expiration )` - Toggle boolean value\n\n### Cache Class Methods (High-Level Patterns)\n\n**Remember Pattern:**\n- `remember( $key, $callback, $expiration )` - Get from cache or compute with callback\n- `remember_by( $prefix, $callback, $expiration, ...$args )` - Remember with auto-generated keys\n\n### Transient Class Methods (Low-Level Operations)\n\n**Core Operations:**\n- `exists( $transient )` - Check if transient exists\n- `get( $transient )` - Get transient value\n- `get_with_default( $transient, $default )` - Get with fallback\n- `get_cast( $transient, $type, $default )` - Get with type casting\n- `set( $transient, $value, $expiration )` - Set transient\n- `delete( $transient )` - Delete transient\n\n**Numeric Operations:**\n- `increment_value( $transient, $amount, $expiration )` - Increment counter\n- `decrement_value( $transient, $amount, $expiration )` - Decrement counter\n\n**Utility Methods:**\n- `get_type( $transient )` - Get value type\n- `get_size( $transient )` - Get size in bytes\n- `toggle( $transient, $expiration )` - Toggle boolean value\n- `is_type( $transient, $type )` - Check if value is specific type\n- `is_large( $transient, $size_limit )` - Check if transient exceeds size limit\n\n### Transients Class Methods (Bulk Operations)\n\n**Bulk Operations:**\n- `exists( $names )` - Check multiple transients\n- `get( $names )` - Get multiple transients\n- `set( $transients, $expiration )` - Set multiple transients\n- `delete( $names )` - Delete multiple transients\n\n**Discovery:**\n- `get_all( $include_values )` - Get all transients\n- `get_by_prefix( $prefix, $include_values )` - Get by prefix\n\n**Pattern Deletion:**\n- `delete_by_pattern( $pattern, $type )` - Delete by pattern\n- `delete_by_prefix( $prefix )` - Delete by prefix (convenience method)\n\n## When to Use What\n\n### Use Global Functions for:\n- **Quick caching** without class imports\n- **Theme development** where simplicity matters\n- **Common operations** like remember, increment, toggle\n- **WordPress-style coding** that feels native\n\n```php\n// WordPress-style - clean and simple\n$data  = wp_cache_remember_by( 'user_stats', $callback, HOUR_IN_SECONDS, $user_id );\n$views = wp_transient_increment( 'page_views_' . $post_id );\n```\n\n### Use Cache class for:\n- **Advanced remember patterns** with custom keys\n- **Plugin development** where you import classes anyway\n- **Complex caching scenarios**\n\n```php\n// When you need custom cache keys or complex patterns\n$data = Cache::remember( 'complex_key_' . $hash, $callback, $expiration );\n```\n\n### Use Transient class for:\n- **Direct transient manipulation**\n- **Type validation and size checking**\n- **Advanced numeric operations**\n\n```php\n// When you need type safety and validation\nif ( Transient::is_large( 'dataset' ) || ! Transient::is_type( 'config', 'array' ) ) {\n\t// Handle issues\n}\n```\n\n### Use Transients class for:\n- **Bulk operations** on multiple transients\n- **Cache cleanup** and maintenance\n- **Pattern-based deletion**\n\n```php\n// Administrative and cleanup operations\nTransients::delete_by_prefix( 'temp_' );\n$values = Transients::get( [ 'key1', 'key2', 'key3' ] );\n```\n\n## Supported Type Casting\n\nThe `get_cast()` method supports these types:\n- `'int'` or `'integer'` - Cast to integer\n- `'float'` or `'double'` - Cast to float\n- `'bool'` or `'boolean'` - Cast to boolean\n- `'array'` - Cast to array\n- `'string'` - Cast to string\n\n## Requirements\n\n- PHP 7.4+\n- WordPress 5.0+\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the GPL-2.0-or-later License.\n\n## Support\n\n- [Documentation](https://github.com/arraypress/wp-transient-utils)\n- [Issue Tracker](https://github.com/arraypress/wp-transient-utils/issues)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-transient-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farraypress%2Fwp-transient-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-transient-utils/lists"}