BricksMembers is optimized for performance with automatic caching, efficient data access patterns, and batch processing capabilities. Understanding these optimizations helps when building custom integrations.
Automatic Caching
BricksMembers automatically caches data for optimal performance. When you use API functions to update data, caches are cleared automatically. You rarely need to manage cache manually:
// Cache is managed automatically
// When you call:
brm_core()->add_user_level( $user_id, $level_id );
// Cache is automatically cleared for:
// - User levels
// - User data
// - Related access checks
Efficient Data Access
Use API functions for data access. They’re optimized with caching and efficient queries:
// Efficient: Uses cache
$levels = brm_core()->get_user_levels( $user_id );
// Efficient: Single query with cache
$has_access = brm_user_can_access_content( $user_id, $post_id );
// Efficient: Batched queries
$completed = brm_core()->get_user_completed_posts( $user_id );
Batch Processing
For operations on multiple items, use batch processing functions:
// Recompute structures in batches
brm_schedule_unified_recompute( [
'structures' => [ 'courses' ],
'spm' => true,
'progress' => true,
'drip' => true
] );
Optimizing Your Custom Code
Avoid Unnecessary Queries
// Good: Check access using API function (cached)
if ( brm_user_can_access_content( $user_id, $post_id ) ) {
// Show content
}
// Avoid: Direct database queries
// This bypasses cache and optimization
global $wpdb;
$wpdb->get_results( "SELECT..." );
Use Hooks Efficiently
// Good: Hook into events rather than polling
add_action( 'brm_user_level_added', function( $user_id, $level_id ) {
// React to event immediately
sync_to_external_service( $user_id, $level_id );
}, 10, 2 );
// Avoid: Checking status repeatedly
// Don't poll for changes - use hooks
Batch Operations
// When updating multiple items, batch them
brm_core()->set_user_levels( $user_id, [1, 2, 3] ); // Single operation
Performance Monitoring
Use debug logging to identify performance issues:
// Track operation performance
$start_time = microtime( true );
// Perform operation
$result = brm_core()->get_user_levels( $user_id );
$duration = microtime( true ) - $start_time;
if ( $duration > 0.1 ) {
brm_log_warning( 'Slow operation detected', [
'operation' => 'get_user_levels',
'duration' => $duration
] );
}
Best Practices
- Use API functions: Always use
brm_*functions rather than direct database access - Trust cache management: API functions handle cache automatically
- Use hooks: React to events rather than polling for changes
- Batch operations: Use batch functions for multiple operations
- Monitor performance: Use our query tool on the Benchmark page to identify slow operations