Fork me on GitHub

Caching

Defining Service

First step is to define a cache service. This is achived by following convention.

  • Define cache pools (hosts, engines etc..)
  • Cache Factory service
  • Defining your cache service

Putting it all together, this is what it looks like.

        
            # config_ENV.yml
            
            # Define cache pools
            hathoora:
                cache:
                    pools:
                        common_mem: { driver: 'memcache', servers: [{host: "localhost", port: 11211}]}            
                        common_redis: 
                            driver: redis
                            servers:
                            - {host: localhost, port: 6379, database: ~, connection_timeout: 2, read_write_timeout: -1, connection_async: 1, connection_persistent: 1}
                        
                       
            services:
                #Cache Factory service
                cache_factory:
                    class: \hathoora\cache\cache
                    calls:
                        setContainer: [ @container@ ]
                    type: static

                # define your cache services
                cache_common:
                    factory_service: @cache_factory@
                    factory_method: pool
                    factory_method_args: [ "common_mem" ]
                    
                # define multiple cache services
                cache.post:
                    factory_service: @cache_factory@
                    factory_method: pool
                    factory_method_args: [ "common_redis" ]                    
        
    

In this example we have defined two cache services cache_common and cache.post which will use memcache and redis respectively. You can use them like so:

        
            // class extending container
            $cacheCommon = $this->getService('cache_common');
            $cacheCommon->get('someKey');
            
            // or using conatiner
            $cacheCommon = \hathoora\container::getService('cache_common');
            $cacheCommon->get('someKey');
        
    

Cache services are lazy loaded and will be connected only server when needed. You can also define multiple hosts to connect to (for redundancy).

        
            
            # Define cache pools with multiple hosts
            hathoora:
                cache:
                    pools:
                        common_mem: 
                            driver: memcache
                            servers: 
                                 - {host: "localhost" , port: 11211}
                                 - {host: "localhostB" , port: 11212}
                                 - {host: "localhostC" , port: 11214}
                            
                        common_redis:
                            driver: redis
                            servers: 
                                - {host: localhostA, port: 6379, database: ~, connection_timeout: 2, read_write_timeout: -1, connection_async: 1, connection_persistent: 1}
                                - {host: localhostB, port: 6379, database: 2, connection_timeout: 2, read_write_timeout: -1, connection_async: 1, connection_persistent: 1}
                                - {host: localhostC, port: 6379, database: ~, connection_timeout: 2, read_write_timeout: -1, connection_async: 1, connection_persistent: 1}
        
    

Key-Value Commands

Basic key-value commands are shown below. You can use redis or memcache for this purpose.

        
            // following would work for both memcache & redis services
            $cacheService = $this->getService('service_name');
            
            $cacheService->get($key);
            $cacheService->set($key, $data, $expire);
            $cacheService->delete($key);
            $cacheService->increment($key, $value);
            $cacheService->decrement($key, $value);
        
    

Engine Specifc Commands

You can specify engine specific commands as well, but they no longer will be interchangeable between memcache & redis based services.

Following showns Memcache specific commands. Memcache based service is extending Memcache class.

        
            $memcacheService = $this->getService('cache_common');
            
            $memcacheService->setCompressThreshold(...);
            $memcacheService->flush(...);
            $memcacheService->replace(...);
        
    

Following showns Redis specific commands - Redis based service is extending Predis\Client.

        
            $redisService = $this->getService('cache.post');
            
            $redisService->mset(...);
            $redisService->mget(...);
            $redisService->hmset(...);
            $redisService->pipe(...);
            ....
        
    

Debugging

This section shows how to enable profiling for debugging purposes.

If you want to view the actual output & input to cache services, then you can enable it by adding debug=1 like so:

        
            # config_ENV.yml
            hathoora:
                cache:
                    pools:
                        common_mem: { debug: 1, driver: 'memcache', servers: [{host: "localhost", port: 11211}]}            
                        common_redis: { debug: 1, driver: 'redis', servers: [{host: "localhost", port: 6379}]}       
        
    
Page Took:         5.89 msec
Page Memory:   520.62k
docs / viewController / view (200)
Request UUID: 59e90704c75755.25377675
Get
Post
Cookies
Files
Session
Server
Response
No data.
No data.
No data.
No data.
No data.
REDIRECT_HATHOORA_ENV
prod
REDIRECT_STATUS
200
HATHOORA_ENV
prod
HTTP_HOST
www.hathoora.org
HTTP_X_FORWARDED_FOR
54.198.143.210
HTTP_X_REAL_IP
54.198.143.210
HTTP_CONNECTION
close
HTTP_ACCEPT_ENCODING
x-gzip, gzip, deflate
HTTP_USER_AGENT
CCBot/2.0 (http://commoncrawl.org/faq/)
HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_IF_MODIFIED_SINCE
Wed, 23 Aug 2017 00:38:35 GMT
PATH
/sbin:/usr/sbin:/bin:/usr/bin
SERVER_SIGNATURE
Apache/2.2.15 (CentOS) Server at www.hathoora.org Port 80
SERVER_SOFTWARE
Apache/2.2.15 (CentOS)
SERVER_NAME
www.hathoora.org
SERVER_ADDR
192.168.1.11
SERVER_PORT
80
REMOTE_ADDR
207.126.164.119
DOCUMENT_ROOT
/var/websites/hathoora.org/docroot
SERVER_ADMIN
webmaster@hathoora.org
SCRIPT_FILENAME
/var/websites/hathoora.org/docroot/index.php
REMOTE_PORT
33984
REDIRECT_URL
/docs/v1/caching
GATEWAY_INTERFACE
CGI/1.1
SERVER_PROTOCOL
HTTP/1.0
REQUEST_METHOD
GET
QUERY_STRING
REQUEST_URI
/docs/v1/caching
SCRIPT_NAME
/index.php
PHP_SELF
/index.php
REQUEST_TIME_FLOAT
1508443908.815
REQUEST_TIME
1508443908
Status: 200
Headers:
Content-Type
text/html; charset=UTF-8
Configutation
Key Value
app.admin
Array
(
    [pattern] => /admin(|/)
    [directory] => hathoora
    [namespace] => hathoora\admin
)
app.docs
Array
(
    [pattern] => /docs(|/)
    [directory] => hathoora
    [dispatcher] => Array
        (
            [class] => customDispatcher
            [method] => dispatch
        )

)
app.site
Array
(
    [default] => 1
)
hathoora.logger
Array
(
    [profiling] => Array
        (
            [enabled] => 1
        )

    [logging] => Array
        (
            [enabled] => 1
            [level] => DEBUG
        )

    [webprofiler] => Array
        (
            [enabled] => 1
            [system] => 
            [show_redirects] => 
        )

)
hathoora.template
Array
(
    [engine] => Array
        (
            [name] => Stuob
        )

)
hathoora.database
Array
(
    [default] => mysql://dbuser:dbpassword@dbhost:3306/dbname
)
services.container
Array
(
    [class] => \hathoora\container
    [method] => getContainer
)
Logging (14)
hathoora.logger.logging.enabled: 1
# Time (msec) Level Memory (KB) Message
1 2.311 DEBUG 309.77 Configuration loaded:
Array
(
    [0] => app_prod.yml
    [1] => config_prod.yml
)
2 2.586 DEBUG 323.38 Listener "kernel.terminate[webprofiler]" has been added because of hathoora.logger.webprofiler.enabled
3 2.763 DEBUG 326.77 Event kernel.ready has been registered.
4 2.957 DEBUG 335.13 Calling App dispatcher (Array ( [class] => customDispatcher [method] => dispatch ) )...
5 3.117 DEBUG 349.55 Event kernel.route has been registered.
6 3.354 DEBUG 378.91 Event kernel.controller has been registered.
7 3.405 DEBUG 379.85 Calling controller (\docs\controller\viewController::view) with following params
Array
(
    [0] => v1
    [1] => caching
)
8 3.990 INFO 467.20 Template (docs/v1/caching.tpl.php) loaded
9 4.159 INFO 481.71 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/htmlMeta.tpl.php) loaded
10 4.251 INFO 482.96 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/flashMessages.tpl.php) loaded
11 5.491 INFO 440.24 Template (docs.tpl.php) fetched.
12 5.551 DEBUG 439.84 Event kernel.response has been registered.
13 5.592 DEBUG 441.02 Event kernel.terminate has been registered.
14 5.708 DEBUG 444.24 Event kernel.terminate->webprofiler has been notified.
Profiling
template
# Time (msec) Name Cached Took (msec)
1 3.500 docs.tpl.php 0 1.876