Butterfly
  • Butterfly
  • Introduction
  • Changelog
  • Admin Panel
  • Audit Logs
  • Cloud
  • Commands
  • Data Manager
  • Data Types
  • Database
  • Designs
  • Introduction
  • Features
  • File Storage
  • Frontend
  • Generators
  • Helpers
  • Installation
  • Items
  • Migrations
  • Modules
  • Notification
  • Performance
  • Queue
  • Security
  • Settings
  • Smarty Helpers
  • Streams
  • Testing
  • Tutorials
  • Upgrade
  • Usage of Validator
  • Webservice
  • Swagger API Documentation Generator
  • CLI Commands
Powered by GitBook
On this page
  • Configuration
  • Redis
  • Database
  • Memcached
  • Apc
  • Using The Cache Client
  • Cache Operations
  • GET
  • SET
  • DELETE
  • INCREMENT
  • DECREMENT
  • FLUSH

Introduction

PreviousButterflyNextChangelog

Last updated 5 months ago

Butterfly uses multi-layer cache management. It's possible to use multiple caches at the same time. You can configure multiple caches and use it. Butterfly uses APC as the primary cache for the configuration files.

Default cache driver is registry on application cycle but you can use the following cache drivers:

  • Redis

  • Memcached

  • Apc

  • Database

When you check Redis and other cache implementations you will see that it's written to make developer comfortable whether you are using Redis or Memcached. We have built a system with same behaviours independent from which driver you use.

Configuration

Butterfly Cache Implementation usages are similar to Butterfly Database implementation.

The cache configurations are stored in app/config/cache.php. Configurations can be customized by domain name with subfolders.

Redis

Before enable redis cache, please check installed.

Example configuration:

<?php

return [
    'default' => 'backend', // default cache
    
    'backend' => [
        'adapter' => 'Redis',
        'server' => [ 
            'scheme' => 'tcp',
            'host'   => '10.0.0.1',
            'port'   => 6379,
        ],
        'password' => 'SECRET_PASSWORD',
        'prefix' => 'be:',
        'database' => 10,    
    ],
    
    'frontend' => [
        'adapter' => 'Redis',
        'server' => 'tcp://10.0.0.1:6379',
        'prefix' => 'fe:',
        'password' => 'SECRET_PASSWORD',
        'database' => 10,     
    ],
    
    'external' => [
        'adapter' => 'Redis',
        'server' => [
            'scheme' => 'tcp',
            'host'   => '1.1.1.1',
            'port'   => 6379,
        ],
        'prefix' => null,
        'password' => 'SECRET_PASSWORD',
        'database' => 0,     
    ],
];

Database

Database cache needs to database_cache table. You can use following migration to create the table.

db()->schema('database_cache')->object(function(\Butterfly\Framework\Data\ButterflyObject $object) {
    $object->setDescription('Butterfly Database Cache');
    $object->string('cache_key', 'Cache Key');
    $object->textarea('value', 'Value');
    $object->integer('expire', 'Expire In Seconds');
    $object->datetimeMysql('created_at');
    $object->unique(['cache_key']);
    return $object;
});

Example configuration for database:

return [
    // other cache configurations in cache.php

    'foo' => [ 
        'driver' => 'Database',
        'prefix' => 'docs:'
    ]
];

Sample usage for the configuration:

cache('foo');

Memcached

Example configuration for memcached:

return [
    // other cache configurations in cache.php

    'bar' => [ 
        'driver' => 'Memcached',
        'servers' => [
            ['127.0.0.1', 11211]
        ],
        'prefix' => 'docs:'
    ]
];

Sample usage for the configuration:

cache('bar');

Apc

Example configuration for apc:

return [
    // other cache configurations in cache.php

    'baz' => [ 
        'driver' => 'Apc',
        'prefix' => 'docs:'
    ]
];

Sample usage for the configuration:

cache('baz');

Using The Cache Client

Cache client can be reached using cache() helper. You can also reach defined cache drivers using the cache alias as the first parameter.

cache(); // Cache Driver defaults to default cache configuration.

Following function call will return a cache client connected to cache external defined in configuration.

cache('external');

Cache Operations

GET

$foo = cache()
    ->get('foo');

SET

$value = 'bar';
cache()
    ->set('foo', 'bar');

DELETE

cache()
    ->delete('foo');

INCREMENT

Increment cached values with optional step amount.

cache()
    ->increment('foo', 1);

DECREMENT

Decrement cached values with optional step amount.

cache()
    ->decrement('foo', 1);

FLUSH

To clear all cache, use flush method to flush related cache source.

cache()
    ->flush();

[!TIP] Further information for parameters and options, please read documentation

Before enable memcached cache, please check installed and enabled.

Before enable apc cache, please check installed and enabled.

predis/predis package
predis/predis
memcached pecl package
apc pecl package