Using local cache along with Doctrine ORM - php

I was working with Silex and Doctrine ORM. To make my database queries faster, I wanted to have a caching of some sort.
I looked at PhpFastCache - which provides a good caching framework - but does not really integrate with Doctrine. The best part about this is that I can have a local cache independent of any external service - like memcached. Since I have a small site which is hosted on shared host, I cannot spend money on having a service on cloud.
I also looked at existing cache providers for Doctrine ORM and all of them use external cache service.
The last thing I know I would have to do is write a provider myself using the PhpFastCache, but just wanted to make sure that there is no alternative online that I can use. I have tried my best by searching online all day today, but I just wanted to make sure.
Just to add: I have looked at APC and Memcache, but I have my site on shared hosting, and I would need a dedicated hosting for installing the PECL modules for APC/Memcache :(.

Doctrine includes quite a few cache drivers that do not seem to be documented. There is not one for PhpFastCache, but there are two that cache directly to the filesystem. Check out FilesystemCache and PhpFileCache. You can see the full list in the repository.
If I had to guess, I'd say that FilesystemCache is what you want. It stores serialized data in a plain file. PhpFileCache stores it as a PHP file, and then uses include to read it later. That means it has to be parsed by PHP on read, which is probably slower unless you use a PHP bytecode cache like APC.
Neither solution will be as fast as something like Memcache since they both read from the filesystem instead of memory, but they should provide an optimization for slow database queries that are run often.
Edit: As Kiran Madipally pointed out, it should be easy create your own PhpFastCache driver by extending CacheProvider.

I quickly wrote a provider for PhpFastCache. I have added the gist here:
https://gist.github.com/thephoenics/ee7de9f95bfdf5f6c24f

Related

Shared Redis-Based Session Between Laravel and Legacy Software

I'm very new to Redis and fairly new to Laravel so could use some pointers on this.
We have a legacy PHP application that stores sessions to Redis and I'm able to see it in Redis with the default naming convention, ie.: PHPREDIS_SESSION:1bd9ca87f5b606a35891c807857c2fde
We're moving towards a Laravel API framework and as a short-term hybrid solution we want the API layer to be able to recognize and work with the session that's already been created through the legacy application.
I've been making slow progress into understanding Redis and I can see the legacy system's entries in Redis from Laravel (if I connect with a blank prefix), but is there a way to cut through Laravel's specialized handling and have it load the same PHPREDIS_SESSION space?
I've orbited this so many times I'm wondering if I've missed something simple.
Ultimately I got this working simply by updating my .env file with:
REDIS_PREFIX=PHPREDIS_SESSION:
CACHE_PREFIX=
I was hoping to avoid that since it's kind of bleh, but it functions and I suppose a config file is better than forcing Laravel to act against the grain.
Laravel is now recognizing the session being stored by my legacy application and trying to load it. Now I just need to get de/serialization to sync...

Redis for silverstripe 4 caching

I am using SilverStripe 4.2.2.
Wondering how could I configure Redis for caching in SilverStripe 4?
Take a look at pstaender/silverstripe-redis-cache which should make it a lot easier to get started with SilverStripe 4 and Redis right off the bat (since it implements CacheFactory and offers some configuration for SilverStripe). Also nice that it also uses predis, similar to Symfony's cache component noted above.
This happens to be ideal for me as well since one of my primary reasons for using Redis would be for template caching distributed across a cluster. 🙂

Configuring Symfony 2 at Runtime

I'm creating web services for my company using Symfony2. Our company uses a centralized configuration service (Zookeeper/etcd) to manage configuration information for our services. Such as connection/host information for MySQL, Redis, Memcached, etc. The configuration is subject to change randomly through out the day. For instance when MySQL servers are added or removed from our database cluster. So hard coding the configuration in yml/xml is not possible.
So I'm looking for a way to modify the config.yml values when the application boots. Some of the values in the config will be static. For instance Twig and Switfmailer configurations, but other values for Doctrine and Redis need to be set on the fly.
The configuration values cannot be determined until the Symfony application boots, and the values cannot be cached or compiled. I've tried a few things to hook into the boot process and modify the configuration, but nothing works.
Additional Information
An example of the architecture I'm dealing with is described here: http://aredko.blogspot.com/2013/10/coordination-and-service-discovery-with.html Along with services like MySQL and Redis, we also need to discover our own RESTful services. Zookeeper is being used as a service discovery agent. The location (host name) and exact configuration for the services aren't known until runtime/boot.
I'd suggest you to take a look at OpenSkyRuntimeConfigBundle.
The bundle allows you to replace traditional container parameters (that you usually write to parameters.yml) with some logic. This provides you a way to make a query to Zookeeper to check the latest configuration variables and inject them to Symfony2 services without a need to rebuild the container.
As you can write the logic in any possible way, you can also implement local cache for the parameters. ZK reads are performant but always require a network round-trip. If performance is important for you, utilize a cache here too.
I wouldn't even consider running Symfony 2 without a cache if you care about performance.
It sounds like you've not quite identified the best way to compose your infrastructure whilst scaling things up / down. It's hard to be specific without knowing more about the bigger picture, but how about pointing the Symfony 2 db config to a proxy server, and manage the routing at the network level. The app then stays blissfully ignorant of the churn of db servers...

Make a CodeIgniter app use MySQL replication the right way

I have a really nasty problem with a CodeIgniter application which I want to load balance using also MySQL replication.
In other PHP frameworks this would be a 5minute job because you may switch the datasource just before saving the data (CakePHP allows this, for example), but it seems in CodeIgniter that's a different story.
My problem is the application is legacy and I have to load balance it so I have all the hardware setup already done, the filesystem is in sync, the database has a slave and updates it as it should, but I have to make the second CodeIgniter application to read from the slave and write to the master.
I already found a solution but since the application is legacy I wouldn't want to go into its internals because I could destroy something that already works (it's also pretty sensible because people are paying within the app and it's also live which makes things even worse).
The solution I've found is this one which makes you change all the models in the application.
As further information CodeIgniter database driver (CI_DB_mysql_driver in this case) doesn't have any method for switching the connection or something.
Do you have another suggestion for tackling this problem? (besides changing all the models which I find a bit too intrusive)

Output Cache and Redis?

I'm trying to implement the whole page cache in my website. (Just like stackoverflow). I have already implemented the Output Cache, but my friend told me that stackoverflow uses redis as their cache layer and I'm confused about the redis part.
Is redis the same as outputcache? Can I implement outputcache by using redis? (For yii developers, I'm using Yii's outputcache).
Thanks!
Yii's output cache will store the cached content using the active cache component, which can be CDummyCache/CDbCache/CApcCache/CFileCache/CMemCache, etc(what you set in the config file under the components area).
As it stands right now, there is no official CRedisCache component, but there is this extension: http://www.yiiframework.com/extension/rediscache/ which might help you.
Also, since Redis is key/value store and a bit more(though you won't use that bit more at all i guess) you can give CMemCache a try(having in mind you have memcache php extension and memcached daemon installed on your server).
L.E: i also found this for you: https://github.com/phpnode/YiiRedis which seems very neat.

Categories