Updated 23 June 2020
In this blog, we will see how to use Redis as cache management for bagisto.
Redis, which stands for Remote Dictionary Server, is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue.
When the first time page is loaded, a database is queried on the server. Redis caches the query. Next time other user loads the page the results are provided from the redis without quering the actual database. It implements a persistent object cache (no expiration). An object cache works by caching the SQL queries in memory which are needed to load the web page. When the data of main database server is updated, then the corresponding key in the Redis is invalidated. So the it provides the updated data instead of caching the data. If a query is not available in Redis, the database provides the result and it adds the result to its cache.
For more info regarding Redis click here
In this demo, we are using Redis on the same server, However it is recommended to use Redis on separate server for better performance.
Step1. Install Redis:-
1 2 |
apt update apt install redis-server |
Step2. Install the predis/predis package via composer.
1 |
composer require predis/predis |
Step3. The Redis configuration for your application is located in the config/database file. Within this file, you will see a redis
array containing the Redis servers utilized by your application. Now Add the cache block to the array like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ], |
Step4. The cache configuration file for bagisto is located inside config/cache.php Now open this file and make the following changes:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php return [ /* |-------------------------------------------------------------------------- | Default Cache Store |-------------------------------------------------------------------------- | | This option controls the default cache connection that gets used while | using this caching library. This connection is used when another is | not explicitly specified when executing a given caching function. | | Supported: "apc", "array", "database", "file", "memcached", "redis" | */ 'default' => env('CACHE_DRIVER', 'redis'), /* |-------------------------------------------------------------------------- | Cache Stores |-------------------------------------------------------------------------- | | Here you may define all of the cache "stores" for your application as | well as their drivers. You may even define multiple stores for the | same cache driver to group types of items stored in your caches. | */ 'stores' => [ 'apc' => [ 'driver' => 'apc', ], 'array' => [ 'driver' => 'array', 'database' => [ 'driver' => 'database', 'table' => 'cache', 'connection' => null, ], 'file' => [ 'driver' => 'file', 'path' => storage_path('framework/cache/data'), ], 'memcached' => [ 'driver' => 'memcached', 'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), 'sasl' => [ env('MEMCACHED_USERNAME'), env('MEMCACHED_PASSWORD'), ], 'options' => [ // Memcached::OPT_CONNECT_TIMEOUT => 2000, ], 'servers' => [ [ 'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => 100, ], ], ], 'redis' => [ 'driver' => 'redis', 'connection' => 'cache', ], ], 'prefix' => env( 'CACHE_PREFIX', \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel'), '_').'_cache' ), ]; |
Step5. Now the final step, go to bagisto root, edit the .env file and make the following changes:-
1 |
CACHE_DRIVER=redis |
and we are done here, Now it’s time to test whether redis is working as cache management for our bagisto or not.
The best and most simplest way to check is by running;-
1 |
redis-cli monitor |
You will get all the insights of how and which database redis is using to storing the cache. Now to check whether redis database is storing the cache use the following commands as shown in the image below
Perfect!!! We have successfully integrated redis for cache management in bagisto.
In the next blog we will see how to use redis as session management for bagisto.
Also, for any suggestions or query contact us at https://bagisto.uvdesk.com/en/
Thanks for reading the article
Stay Connected !!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.