Cache

From DreamFactory
Jump to: navigation, search

Cache Service extends the DreamFactory REST API with a generic interface to use various Caching backends such as Redis, Memcached, local file based cache seamlessly without having to worry about the differences among them.

Cache Services are native services of DreamFactory and are supported by features like role-service-access, lookup usage, live API documentation, and caching. Once your cache service is configured in DreamFactory, all the configuration details are hidden from your client. They are securely stored on your DreamFactory instance database. This allows for a simple and consistent way to access your local and remote cache services via the DreamFactory REST API.

There are currently three types of cache services in DreamFactory - Local, Redis, and Memcached.

Local Cache Service

The Local Cache Service, by default, allows you to use the file based local storage of the server where your DreamFactory instance is hosted. You can configure your DreamFactory instance to use other cache backends like Redis, Memcache, APC etc as Local Cache service. See configuration below for more details.

Configuration

Local Cache service is managed via the api/v2/system/service API endpoint under the system service and have the service_type cache_local. You can retrieve the full service type information using the API api/v2/system/service_type/cache_local.

Below is the format of a typical Local Cache service configuration.

{
    //Choose a URL safe service name
    "name": "files",
    //Choose a label for your service
    "label": "Local Cache Service",
    //A short description of your service
    "description": "Service for using local cache backends.",
    //Boolean flag to activate/inactivate your service
    "is_active": true,
    //Service type,
    "type": "cache_local",
    "config": {
        "store": "file",
        "default_ttl": 300
    }
}

The following describes the configuration elements of this service type.

store

Text. Optional. Default is file if left blank. This is one of the stores configured in your DreamFactory instance under config/cache.php file. Below is how the default stores configuration looks. You can pick any of these (apc, array, database, file, memcached, redis) as your store for your Local Cache service.

Note: If your choose array store then your cache is not going to be persisting. If you choose redis store then you will need to provide your redis configuration under config/database.php file. If you use memcached store then you will need to set the MEMCACHED_HOST, MEMCACHED_PORT, MEMCACHED_WEIGHT in the .env file.

    'stores'  => [
        'apc'                                      => [
            'driver' => 'apc',
        ],
        'array'                                    => [
            'driver' => 'array',
        ],
        'database'                                 => [
            'driver'     => 'database',
            'table'      => 'cache',
            'connection' => null,
        ],
        'file'                                     => [
            'driver' => 'file',
            'path'   => env('DF_CACHE_PATH', storage_path('framework/cache')),
        ],
        'memcached'                                => [
            'driver'  => 'memcached',
            'servers' => [
                [
                    'host'   => env('MEMCACHED_HOST','127.0.0.1'),
                    'port'   => env('MEMCACHED_PORT', 11211),
                    'weight' => env('MEMCACHED_WEIGHT', 100),
                ],
            ],
        ],
        'redis'                                    => [
            'driver'     => 'redis',
            'connection' => 'default',
        ]
    ]

default_ttl

Integer. Optional. Defaults to 300 minutes. This is the Time to Live (TTL) value in minutes for your cache before it expires.

Redis Cache Service

The Redis Cache Service allows you to use a remote or local (installed on the same server as your DreamFactory instance) Redis server as your cache backend.

Configuration

Redis Cache service is managed via the api/v2/system/service API endpoint under the system service and have the service_type cache_redis. You can retrieve the full service type information using the API api/v2/system/service_type/cache_redis.

Below is the format of a typical Redis Cache service configuration.

{
    //Choose a URL safe service name
    "name": "redis-cache",
    //Choose a label for your service
    "label": "Redis Cache Service",
    //A short description of your service
    "description": "Service for using Redis server.",
    //Boolean flag to activate/inactivate your service
    "is_active": true,
    //Service type
    "type": "cache_redis",
    "config": {
        "host": "127.0.0.1",
        "port": 6379,
        "password": null,
        "database_index": 0,
        "default_ttl": 300,
        "options": []
    }
}

The following describes the configuration elements of this service type.

host

Text. Required. Hostname/IP address of your Redis server.

port

Integer. Optional. Defaults to 6370. Port number of your Redis server.

password

Text. Optional. Password for your Redis server if it requires authentication.

database_index

Integer. Optional. Defaults to 0. Your Redis database index.

default_ttl

Integer. Optional. Defaults to 300 minutes. This is the Time to Live (TTL) value in minutes for your cache before it expires.

options

Object/Array. Optional. An array of options for the Redis connection. For details see https://github.com/nrk/predis/wiki/Quick-tour#connection

Memcached Cache Service

The Memcached Cache Service allows you to use a remote or local (installed on the same server as your DreamFactory instance) Memcached server as your cache backend.

Configuration

Memcached Cache service is managed via the api/v2/system/service API endpoint under the system service and have the service_type cache_memcached. You can retrieve the full service type information using the API api/v2/system/service_type/cache_memcached.

Below is the format of a typical Memcached Cache service configuration.

{
    //Choose a URL safe service name
    "name": "mc-cache",
    //Choose a label for your service
    "label": "Memcached Cache Service",
    //A short description of your service
    "description": "Service for using Memcached server.",
    //Boolean flag to activate/inactivate your service
    "is_active": true,
    //Service type
    "type": "cache_memcached",
    "config": {
        "host": "127.0.0.1",
        "port": 11211,
        "default_ttl": 300
    }
}

The following describes the configuration elements of this service type.

host

Text. Required. Hostname/IP address of your Memcached server.

port

Integer. Optional. Defaults to 11211. Port number of your Memcached server.

default_ttl

Integer. Optional. Defaults to 300 minutes. This is the Time to Live (TTL) value in minutes for your cache before it expires.