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:         3.29 msec
Page Memory:   520.55k
docs / viewController / view (200)
Request UUID: 59ef1da542b086.76964914
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
hathoora.org
HTTP_X_FORWARDED_FOR
54.166.203.76
HTTP_X_REAL_IP
54.166.203.76
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
Fri, 18 Aug 2017 01:00:29 GMT
PATH
/sbin:/usr/sbin:/bin:/usr/bin
SERVER_SIGNATURE
Apache/2.2.15 (CentOS) Server at hathoora.org Port 80
SERVER_SOFTWARE
Apache/2.2.15 (CentOS)
SERVER_NAME
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
34272
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
1508842917.272
REQUEST_TIME
1508842917
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 1.674 DEBUG 309.70 Configuration loaded:
Array
(
    [0] => app_prod.yml
    [1] => config_prod.yml
)
2 1.791 DEBUG 323.30 Listener "kernel.terminate[webprofiler]" has been added because of hathoora.logger.webprofiler.enabled
3 1.865 DEBUG 326.70 Event kernel.ready has been registered.
4 1.952 DEBUG 335.05 Calling App dispatcher (Array ( [class] => customDispatcher [method] => dispatch ) )...
5 2.021 DEBUG 349.48 Event kernel.route has been registered.
6 2.137 DEBUG 378.83 Event kernel.controller has been registered.
7 2.158 DEBUG 379.77 Calling controller (\docs\controller\viewController::view) with following params
Array
(
    [0] => v1
    [1] => caching
)
8 2.420 INFO 467.12 Template (docs/v1/caching.tpl.php) loaded
9 2.510 INFO 481.63 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/htmlMeta.tpl.php) loaded
10 2.566 INFO 482.88 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/flashMessages.tpl.php) loaded
11 3.118 INFO 440.16 Template (docs.tpl.php) fetched.
12 3.144 DEBUG 439.76 Event kernel.response has been registered.
13 3.162 DEBUG 440.95 Event kernel.terminate has been registered.
14 3.210 DEBUG 444.16 Event kernel.terminate->webprofiler has been notified.
Profiling
template
# Time (msec) Name Cached Took (msec)
1 2.203 docs.tpl.php 0 0.857