Using the cache component of Laravel ( Illuminate/Cache ) as caching backend for my app, how can I register a new custom cache driver? Since I don’t use Laravel at all (only Illuminate/Cache), I can’t add it to the service provider and the Cache facade returns and error.
Note that I’m successfully using the defaults drivers (file, memcached, redis) by passing the config and such inside an empty Illuminate\Container\Container using the singleton function:
EDIT - Sample code how I get a cache store:
$app = new Illuminate\Container\Container();
// Where $config is an array of config values
$app->singleton('config', function() use ($config) {
return $config;
});
$app->singleton('files', function() {
return new Illuminate\Filesystem\Filesystem();
});
$cacheManager = new CacheManager($app);
// Where $storeName is linked to the configs values
return $cacheManager->store($storeName);
Firstly, you will need to make sure you driver extends the Illuminate\Contracts\Cache\Store interface.
Then you should just be able to do something like:
$cacheManager->extend('you-custom-driver-name', function ($app) use($cacheManager) {
return $cacheManager->repository(new YourCustomDriver);
});
https://laravel.com/docs/5.4/cache#adding-custom-cache-drivers
Hope this helps!
Related
I'm trying to test my own Rest-API using a Client-library which handles the requests for me.
I've written a helper in which the Client gets initialized and a action is added called "wantToGetUserOverClient($userId)". The client does the request and returns a JSON, that works as intended when I var_export() the JSON.
I've tried to extend the Rest module and assigning the public response field directly but get an exception:
[ModuleException] PhpBrowser: Page not loaded. Use `$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it
My Question:
What is the best way to save the Client response in the Codeception REST module response so I can use the already existing JSON-actions.
For example:
$I->wantToGetUserOverClient(1234);
$I->canSeeResponseIsJson();
without loading a Page withe the PhpBrower.
Thanks in advance.
My helper:
class ApiHelper extends \Codeception\Module\REST
{
private $backendApi;
public function __construct(ModuleContainer $moduleContainer, $config = null)
{
parent::__construct($moduleContainer, $config);
$this->backendApi = new BackendRestAPI();
}
public function wantToGetUserOverClient($userId)
{
$this->response = $this->backendApi->user()->one($userId);
}
}
You are misusing REST module.
But if you insist, you have to replace PhpBrowser with your module.
Configuration:
REST:
depends: YourModule
To make seeResponseIsJson work, YourModule must extend Codeception\Lib\InnerBrowser and implement _getResponseContent method.
Whenever I load a page, I can see Laravel reading a great amount of data from the /storage folder.
Generally speaking, dynamic reading and writing to our filesystem is a bottleneck. We are using Google App Engine and our storage is in Google Cloud Storage, which means that one write or read is equal to a "remote" API request. Google Cloud Storage is fast, but I feel it's slow, when Laravel makes up to 10-20 Cloud Storage calls per request.
Is it possible to store the data in the Memcache instead of in the /storage directory? I believe this will give our systems a lot better performance.
NB. Both Session and Cache uses Memcache, but compiled views and meta is stored on the filesystem.
In order to store compiled views in Memcache you'd need to replace the storage that Blade compiler uses.
First of all, you'll need a new storage class that extends Illuminate\Filesystem\Filesystem. The methods that BladeCompiler uses are listed below - you'll need to make them use Memcache.
exists
lastModified
get
put
A draft of this class is below, you might want to make it more sophisticated:
class MemcacheStorage extends Illuminate\Filesystem\Filesystem {
protected $memcached;
public function __construct() {
$this->memcached = new Memcached();
$this->memcached->addServer(Config::get('view.memcached_host'), Config::get('view.memcached_port');
}
public function exists($key) {
return !empty($this->get($key));
}
public function get($key) {
$value = $this->memcached->get($key);
return $value ? $value['content'] : null;
}
public function put($key, $value) {
return $this->memcached->set($key, ['content' => $value, 'modified' => time()]);
}
public function lastModified($key) {
$value = $this->memcached->get($key);
return $value ? $value['modified'] : null;
}
}
Second thing is adding memcache config in your config/view.php:
'memcached_host' => 'localhost',
'memcached_port' => 11211
Last thing you'll need to do is to overwrite blade.compiler service in one of your service providers, so that it uses your brand new memcached storage:
$app->singleton('blade.compiler', function ($app) {
$cache = $app['config']['view.compiled'];
$storage = $app->make(MemcacheStorage::class);
return new BladeCompiler($storage, $cache);
});
That should do the trick.
Please let me know if you see some typos or error, haven't had a chance to run it.
I'm using the Phalcon flash service to store temporary messages between HTTP redirects.
It worked great until I recently changed to a database session adapter. Now flash messages are being stored in the database but are not being deleted.
I don't want these messages to touch the database so I setup the previous session method in the DI but under a new tempSession entry.
public function initTempSession($options = [])
{
$this->di->set(
'tempsession',
function () {
$tempSession = new PhSession();
$tempSession->start();
return $tempSession;
},
true
);
}
How can I configure the flash service to use this session function, rather than the 'default' database adapter?
I have been looking into how to create a plugin system with Phalcon. I have checked INVO tutorial, Events Management to be more specific. It doesn't look like this is what I exactly need.
In this example it is loading the plugins manually and before establishing database connection.
I need to be able to access the database to check if the plugin actually installed and activated. So I can retrieve settings of installed & activated plugins as well as dynamically add them to the app.
I need to be able to attach the plugins pretty much anywhere; controllers (before / after / within method execution), models (before, after, within method execution) etc.
Does Phalcon provide such feature or do I have to ahead and try to create my own logic without using any framework feature?
Do you have any examples of what you would have plugins do? Or specifics about where Events Management is lacking?
I think you should be able able to get a very flexible system using dependency injection and events management.
It appears you can use the phalcon models before running the application, as long as you place the code after setting the db in the injector.
$plugins = \Plugin::find([
'active = :active:',
'bind'=>[
'active'=>1
]
]);
foreach($plugins as $plugin){
if(file_exists($plugin->filename)){
include $plugin->filename;
}
}
and in the file you could have code to subscribe to events, and/or add new dependencies.
// plugin file - for a db logger
$eventsManager = new \Phalcon\Events\Manager();
$logger = new \PhalconX\Logger\Adapter\Basic();
$profiler = $phalconDi->getProfiler();
$eventsManager->attach('db', function($event, $phalconConnection) use ($logger, $profiler) {
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($phalconConnection->getSQLStatement());
$logger->log($phalconConnection->getSQLStatement(), \Phalcon\Logger::INFO, $phalconConnection->getSQLVariables());
}
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
or
class myHelper{ ... }
$phalconDi->set('myHelper', function() use ($phalconConfig, $phalconDi) {
$helper = new myHelper();
$helper->setDi( $phalconDi );
return $helper;
});
Hi I am new to cassandra database. I am trying to do a project in codeigniter with cassandra database. I have downloaded the phpcassandra files through below link
https://github.com/mauritsl/php-cassandra.
When I am trying to autoload my casssandra.php in codeigniter I got Non-existent class: Cassandra error. Why I got this error and how to solve the issue?
You will need to create a wrapper for it if you wish to use it as a library.
I would suggest you take the composer route.
You can check on packagist for a suitable library.
If you use this particular library phpcassa this is how you would go about getting it to work in Codeigniter.
composer.json
{
"require": {
"php": ">=5.3.0",
"thobbs/phpcassa": "1.1.0"
}
}
index.php
require "../composer/autoload.php";
system/core/codeigniter.php
// Where codeigniter starts to load the Main Controller
// $cassandraDB will be in the GLOBAL scope, so you may want to write a wrapper
$cassandraDB = new ConnectionPool('localhost');
// Then right after this
// if (class_exists('CI_DB') AND isset($CI->db))
// {
// $CI->db->close();
// }
$cassandraDB->close();
User Model
implement it how you wish in your models
public function __construct()
{
//YOU MAY NEED TO PASS $cassandraDB AS A DEPENDANCY!!
$this->users = new ColumnFamily($cassandraDB, 'Standard1');
}