Obtaining All Cache Keys in Zend-Cache - php

Here is my cache initialization code:
use Zend\Cache\StorageFactory;
$cache = StorageFactory::factory(array(
'adapter' => array(
'name' => 'filesystem',
// With a namespace we can indicate the same type of items
// -> So we can simple use the db id as cache key
'options' => array(
'namespace' => 'dbtable',
'cache_dir' => Pluto::path('cache')
),
),
'plugins' => array(
// Don't throw exceptions on cache errors
'exception_handler' => array(
'throw_exceptions' => false
),
// We store database rows on filesystem so we need to serialize them
'Serializer'
)
));
What Id like to know is how do I obtain all of the cache keys we have inside this cache object
For example, executing this code now:
$cache->setItem('key1','foo');
$cache->setItem('key2','bar');
$cache->setItem('key3','baz');
And executing this code at a different area/point:
$cache->setItem('key4','foo2');
$cache->setItem('key5','bar2');
$cache->setItem('key6','baz2');
I'd like an array containing ['key1','key2','key3','key4','key5','key6'] which would come presumbly from an internal array of all the keys inside the cache object (including ones that were not affected during this specific request)?

AFAIK, there is no method from zend-cache for retrieving all keys inside cache object. But if you wanna retrieve all keys, you can iterate the object. It is not array, but you can make it as array if you want.
$caches = $cache->getIterator();
$cacheKeys = []
foreach ($caches as $key) {
// $key is the cache key
$cacheKeys[] = $key;
}
print_r($cacheKeys);

Related

Laravel 5 passing page info with array

I'm trying to pass data about the page state (navbar links having active class when you are in that exact page), page title. I do so with an indexed array $pageInfo, however I am getting a syntax error and doen't know where?
Also do you think this is a good method or should I use view->share() instead?
public function clases()
{
$pageInfo[] =
(
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
);
return view('clases.list', compact('pageInfo'));
}
public function domicilio()
{
$pageInfo[] =
(
'page_title' => 'Clases a domicilio',
'menu_active' => 'Clases',
'sub_menu_active' => 'Clases a domicilio',
);
return view('clases.domicilio', compact('pageInfo'));
I suggest you read PHP basic syntax.
Basically you want to do this:
$pageInfo =
[
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
];
Arrays have a syntax of [key => val, ...] in PHP, you're using () as it seems.
Also $someArray[] = someValue, will append the someValue to an existing array, in your case that would create another, unwanted level of your array.
And last, you're not ending the domicilio() function. But I'll assume you just didn't paste it in (you should add } at the end, if that's not the case).

Laravel return array value from app/config/package folder

Hi I am using a laravel package https://github.com/greggilbert/recaptcha and I have published the config files where I need to store my public and private keys for my recaptcha. I want to call this elsewhere in my app in an attempt to process this via AJAX . I've tried to call this as
dd(Config::get('packages.greggilbert.recaptcha')['public_key']);
however this returns as null. The array is kept in the following folder:
app/config/packages/greggilbert/recaptcha/config.php
the array is built as so:
<?php
return array(
'public_key' => 'publickey',
'private_key' => 'privatekey',
'template' => '',
'driver' => 'curl',
'options' => array(
'curl_timeout' => 1,
),
'version' => 2,
);
Any ideas how I can retrieve the public_key value??
Retrieving configs from a package works a bit different. You use a so-called namespace:
Config::get('recaptcha::public_key');

Codeigniter add new item to current session userdata array

So I have this array:
$data = array(
'item_1' => $this->input->post('item_1'),
'item_2' => $this->input->post('item_2'),
'item_3' => $this->input->post('item_3')
);
$this->session->set_userdata( 'items', $data );
And I want to add a new item to that array, so the updated array of userdata will be like this:
$data = array(
'item_1' => $this->input->post('item_1'),
'item_2' => $this->input->post('item_2'),
'item_3' => $this->input->post('item_3'),
'item_4' => $this->input->post('item_4')
);
$this->session->set_userdata( 'items', $data );
How to do that, Thank you
Follow these steps to add data to the current session:
$data = $this->session->userdata('items');
$data['item_4'] = $this->input->post('item_4');
$this->session->set_userdata('items', $data);
Here, we first take out the current session items in an array, add another item to the array, you can do a array_push() but I prefer the above. Now, set the session back with the updated data.
Got this from ellislab:
Adding Custom Session Data
A useful aspect of the session array is that you can add your own data to it and it will be stored in the user's cookie. Why would you want to do this? Here's one example:
Let's say a particular user logs into your site. Once authenticated, you could add their username and email address to the session cookie, making that data globally available to you without having to run a database query when you need it.
To add your data to the session array involves passing an array containing your new data to this function:
$this->session->set_userdata($array);
Where $array is an associative array containing your new data. Here's an example:
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
If you want to add userdata one value at a time, set_userdata() also supports this syntax.
$this->session->set_userdata('some_name', 'some_value');
So in essence you should be able to create the item you want to add and then add it to the session by using $this->session->set_userdata("item_4", "item_4"); or $this->session->set_userdata($newdata) if you want to add many items .

How to create an empty field if the field doesn't exist in MongoDB

I have a document that is created on weakly bases (field delimeter hold date) for each user's week.
Every time the user session expires I check to see the user has a document for that particular week (so that all updates dont fail)
I am trying to avoid 2 fetches : 1) a fetch to check if the document exists, 2) create another query that creates or not that document depending on whether it exists already
I was wondering if there is such a function within mongo... Create if doesn't exist otherwise do nothing?
Currently, upsert inserts if it is not there or updates if it is there. Thus, is not a valid solution. I need if ( !exist){ update/insert ) else { do nothing }
public function createUserSocialWallForUser( MongoId $userMongoID, $delimeter ) {
$db = $this->db()->socialWall;
$where = array( 'userId' => $userMongoID,
'delimeter' => $delimeter
);
$data = array( 'userId' => $userMongoID, 'delimeter' => $delimeter, 'event' => array() );
$option = array ('upsert' => true);
$db->update( $where, $data, $option);
}
Sound like a perfect fit for $setOnInsert:
$data = array(
'$setOnInsert' => array(
'userId' => $userMongoID,
'delimeter' => $delimeter,
'event' => array()
)
);
This way the update only sets the values if the upsert needs to do an insert (the not-exists case).
Note that this requires at least v2.4 of MongoDB.

Storing a multidimensional array as an object variable, adding additional keys to the array

So I have a multidimensional array which is stored in an object. I want to add additional keys to this array.
Here's what I have:
$object->pathsArray = array(
"key1" => array('path' => '/some/path/to/some/file.php', 'action' => 'index'),
"key2" => array('path' => '/some/path/to/some/class.php', 'action' => 'method2')
);
And here's what I assumed would work but did not:
$object->pathsArray['key3'] = array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3');
My first workaround:
$newPathsArray = array("key3" => array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3'));
$object->pathsArray = array_merge($object->pathsArray, $newPathsArray);
Another workaround that SHOULD work:
$tempPathsArray = $object->pathsArray;
$tempPathsArray['key3'] = array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3');
$object->pathsArray = $tempPathsArray;
So my question: Is there a simpler syntax (ie: one line solution) or am I forced to bring in a temp. variable, append to that then merge/re-assign the value to the object?
Sorry to write an answer, but I'm not able to comment.
I think that it's not correct to make an attribute public just to use it by that way. The correct thing should be make a setter to fill it, not modifying the class design just for that.

Categories