Menu

Data Utilities and Caching

Relevant source files

This document covers the data handling utilities and caching mechanisms used throughout the Multisite Language Switcher plugin. These components provide efficient data storage, retrieval, and manipulation capabilities for language relationships, SQL query results, and structured data containers.

For information about how options are stored and managed, see Options and Configuration Storage. For details on content type handling, see Content Type Management.

SQL Query Caching System

The plugin implements a comprehensive SQL caching layer through the MslsSqlCacher class, which wraps WordPress database operations with object cache functionality.

MslsSqlCacher Architecture

The MslsSqlCacher serves as a proxy for the WordPress $wpdb object, automatically caching results from get_* methods while allowing direct passthrough for modification operations.

Sources: includes/MslsSqlCacher.php1-108 includes/MslsOptionsQuery.php20-28

Cache Implementation Details

The caching system uses WordPress's object cache with a dedicated cache group msls-cache-group. Cache keys are generated from the calling class and parameters to ensure uniqueness.

ComponentPurposeCache Duration
MslsSqlCacher::create()Factory for cache instancesConfigurable expiration
__call() magic methodIntercepts get_* database callsPer-request or persistent
CACHE_GROUP constantIsolates plugin cache dataN/A

The caching logic only applies to database read operations (get_var, get_results, etc.), allowing write operations to execute immediately without cache interference.

Sources: includes/MslsSqlCacher.php66-74 includes/MslsSqlCacher.php95-107

Data Container Utilities

MslsGetSet - Generic Property Container

The MslsGetSet class provides a foundation for dynamic property management with automatic cleanup of empty values.

Sources: includes/MslsGetSet.php12-116

MslsLanguageArray - Language Mapping Storage

The MslsLanguageArray class manages validated language-to-ID mappings with strict input validation.

The validation ensures data integrity by:

  • Requiring language keys of at least 2 characters
  • Converting values to positive integers
  • Automatically removing invalid entries

Sources: includes/MslsLanguageArray.php23-46 includes/MslsLanguageArray.php56-75

MslsJson - JavaScript Data Container

The MslsJson class structures data for JavaScript consumption in admin interfaces.

Sources: includes/MslsJson.php26-33 includes/MslsJson.php52-76

Usage Patterns in Options Classes

Query Options Caching Integration

The various MslsOptionsQuery subclasses demonstrate the caching system's integration:

Each query class provides parameters through get_params() methods that become part of the cache key, ensuring proper cache isolation between different query contexts.

Sources: includes/MslsOptionsQuery.php44-64 includes/MslsOptionsQueryDay.php39-45

Property Access Patterns

The MslsGetSet foundation enables consistent property access across option classes:

MethodUsage PatternExample
has_value($key)Check for non-empty valuesLanguage link existence
__get($key)Direct property accessRetrieve post ID
__set($key, $value)Store with auto-cleanupSet language relationships
reset()Clear all dataReinitialize options

Sources: includes/MslsGetSet.php95-97 includes/MslsGetSet.php25-31

Performance Considerations

Cache Key Strategy

Cache keys combine class names and parameters to prevent collisions while maintaining readability for debugging:

  • Format: {ClassName}_{Parameters}
  • Parameters are imploded with underscores for arrays
  • Keys are sanitized using esc_attr()

Memory Management

The MslsGetSet class automatically manages memory by:

  • Removing empty array elements in __set()
  • Providing bulk reset functionality
  • Using null coalescing for safe property access

Database Query Optimization

The caching layer reduces database load by:

  • Caching all get_* method results
  • Bypassing cache for write operations
  • Using WordPress's native object cache infrastructure

Sources: includes/MslsSqlCacher.php66-74 includes/MslsGetSet.php67-75