Skip to content

Instantly share code, notes, and snippets.

@jeroendesloovere
Last active October 3, 2016 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroendesloovere/ae53868cbd5517b2c40e7ddaf1db3db6 to your computer and use it in GitHub Desktop.
Save jeroendesloovere/ae53868cbd5517b2c40e7ddaf1db3db6 to your computer and use it in GitHub Desktop.
How to integrate Redis in Fork CMS

How to integrate Redis in Fork CMS

What is Redis?

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Why did we use Redis in Fork CMS?

Our situation

We needed to create a (huge) webshop in Fork CMS, which depends on external software for its ten thousands of products and categories. Unfortunately that external database was missing important information about how the website should show categories and its products. So next to the import of categories and products, We needed to create hard coded extra levels and combinations in the website with a huge cache builder behind it.

Filesystem no go

Our specific category/products cache builder calculates the tree structure for the website and all its products in a category(-filter) once after every import. By default these variables where dropped in a file. Since we expect thousands of visitors and lots of reads to these files we need to sure that they keep on working fast.

How did we integrate Redis in Fork CMS?

In the Fork CMS /app/config/config.yml file you can find the following.

    cache.filesystem.adapter:
        class: League\Flysystem\Adapter\Local
        arguments:
            - %kernel.cache_dir%
    cache.filesystem.filesystem:
        class: League\Flysystem\Filesystem
        arguments:
            - "@cache.filesystem.adapter"
    cache.adapter:
        class: MatthiasMullie\Scrapbook\Adapters\Flysystem
        arguments:
            - "@cache.filesystem.filesystem"
    cache.buffer:
        class: MatthiasMullie\Scrapbook\Buffered\BufferedStore
        arguments:
            - "@cache.adapter"
    cache.pool:
        class: MatthiasMullie\Scrapbook\Psr6\Pool
        arguments:
            - "@cache.buffer"

At first we converted the above to be using Redis. But this gave us some issues in the import script.

Thats why we decided to add extra config rules for redis + cache builder. In the Fork CMS /app/config/config.yml file we added the following.

    # Caching using Redis Adapter
    products_cache.redis.adapter:
        class: Redis
        calls:
            - [connect, ['%redis.server%', '%redis.port%']]
            - [auth, ['%redis.password%']]
            - [select, ['%redis.database_index%']]
    products_cache.adapter:
        class: MatthiasMullie\Scrapbook\Adapters\Redis
        arguments:
            - "@products_cache.redis.adapter"
    products_cache.buffer:
        class: MatthiasMullie\Scrapbook\Buffered\BufferedStore
        arguments:
            - "@products_cache.adapter"
    products_cache.pool:
        class: MatthiasMullie\Scrapbook\Psr6\Pool
        arguments:
            - "@products_cache.buffer"

In the Fork CMS /app/config/parameters.yml file we added the following.

    redis.server:           127.0.0.1
    redis.port:             6379
    redis.password:             # This is by default null
    redis.database_index:   0   # This is by default 0

The next things was to install Redis locally. Here is how I installed Redis locally + MAMP On the server, my hosting provider activated Redis and just gave me the redis.server/port and password.

Once you have installed Redis, the website will fly like never before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment