How to use memcached from codeigniter - codeigniter

How to use memcached from codeigniter, and how to store session data to memcached.
Please help me.
Thanks

Here is the link to my memcached_library for codeigniter
http://github.com/tomschlick/memcached-library
let me know what you think and if you have any issues please raise them in the issues section of the github repository

Codeigniter V2.1.0 supports caching
http://codeigniter.com/user_guide/libraries/caching.html#memcached

Here is an introduction to memcached and PHP:
enhance_php_session_management
As far as using memcached from CI, I imagine you would want to either add the caching code directly into your models, or from your Controllers you would want to check the cache before querying data from a model.

public function index()
{
// manual connection to Mamcache
$memcache = new Memcache;
$memcache->connect("localhost",11211);
$data=$memcache->get("test_key");
if($data){
echo 'cache data:';
var_dump($data);
}else{
$data=$this->db->query("SELECT count(*) as ca FROM table WHERE typ=1 ")->row();
$memcache->set("test_key",$data,false,10); // 10 seconds
echo 'real data:';
var_dump($data);
}
}

Related

Is it ok to use $_SESSION['variables'] in Drupal 8?

I need a way to store temporary data for anonymous users.
Apparently this is not possible with:
\Drupal::service('user.private_tempstore')
Unless you write a custom constructor for the session management and stuff, which seems a little far-fetched to me?
I tried using
\Drupal::service('user.shared_tempstore')
But that saves the temp data for all anonymous users. So it's not linked to a single user.
Using raw $_SESSION['data'] works fine, but I'm not sure if I'm supposed to be doing this in Drupal and how safe/unsafe it is to do this?
Sessions (Drupal 8) are used via the simple Session implementation of SessionInterface interface. See Complete Tutorial of Sessions (Drupal 8).
Example:
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
// set and get session attributes
$session->set('name', 'Yash');
$session->get('name');
// set flash messages
$session->getFlashBag()->add('notice', 'Profile updated');
// retrieve messages
foreach ($session->getFlashBag()->get('notice', array()) as $message) {
echo '<div class="flash-notice">'.$message.'</div>';
}
I am not answering your specific question (regarding $_SESSION) because I have successfully used:
$session = \Drupal::service('user.private_tempstore')->get('your_module');
$session->set('whatever', $whatever);
from within procedural code (i.e. hooks, themes) without problems.
Pay attention that this private tempstore has to be assigned to a module (for the lack of a better way of saying this) which is the purpose of this line
$session = \Drupal::service('user.private_tempstore')->get('your_module')
After you get the private tempostore you can now set and get the session values:
$session->get('whatever');
$session->set('whatever', $whatever);
EDIT
Sorry, you explained correctly. I didn't get the critical part 100% ;)
You can always access the Session object from the request.
$session = \Drupal::request()->getSession();
$session->set('whatever', 'hello');
$value = $session->get('whatever', 'default');
I've been using plain PHP $_SESSION variables for a while now.
Did some research on them and they should be perfectly safe to use.
They're working correctly everywhere I use them and they have been working correctly for a while.
Don't think there's any issue using them in Drupal 8.

get joomla registration form variables?

I'm working on getting joomla registration form values that user enters
at the time of registration. After two days of searching I reached to the
file Joomla2.5.7\components\com_users\controllers\registration.php. In the register() method I
tried to echo $data and $requestData variables but didn't see any output, on registering a new entry. I also tried to echo javascript but was unsuccessful. I'm trying to connect joomla database with my own database, so that whenever new user registers , he is also registered to my website. How can I get the registration form variables, any kind of help is really appreciated.
Ty this
On registration controller you can find a function call to model like this
$return = $model->register($data);
after that just
echo "<pre/>";
print_r($data);
Also in the registration model
components\com_users\model\registration.php
register() method is defined you can check that for more info.
In addition if you want to add the users info to the other DB like your website DB.
The best place to write mysql query is :
// Store the data.
if (!$user->save()) {
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
return false;
}else{
//Your custom mysql query to other DB or tables
}
find the above section in the registration model inside register()method.
Hope this may solve your problem..
I hope this tutorial helps you. You will find it explains the process of joomla registration form very well.
http://youtu.be/2AyCzb2vTaU

Magento Tables and how they work with the database

I been trying to understand Magento and I read many things on the wiki, but I couldn't figure out How does Magento works with database tables? because I didn't see any SQL
I would reccomend read over this blog post from Alan Storm:
http://alanstorm.com/magento_models_orm
He explains quite abit about the Magento ORM system, and in my opinion that entire site is a great resource for any Magneto developer.
If you watch your MySQl log, the calls made by magento can sometimes be 500 lines long or longer ... These calls are dynamically constructed using XML files. The best way to manipulate Magento data manually is to use MAGE:: calls or use a direct database connection by using:
$read = $resource->getConnection('core_read');
$sql = "select * from [YOUR_TABLE] where 1 limit 1";
$result = $read->query($sql);
It's either that or calls that look like:
$value = 'some value';
$item->setData('some_key', $value);
$item->save();
Magento is object oriented, so those are the most commonly accepted and used ways to retrieve/set data in Magento. I hope that helps.
Read chapter 5 onwards from the knowledge base.
You are not really asking a question so no one can help on the specifics, I always find that you learn best by doing, I find the best way to mess around with magento is to create a test.php file in shell/ with the following: (for example)
<?php
require('abstract.php');
class Test extends Mage_Shell_Abstract
{
function run(){ //call your functions here
echo 'running ..';
$this->database();
}
function database() { //you can create as many functions as you like
$entityId = '4449'; //product id
$product=Mage::getModel("catalog/product")->load($entityId);
var_dump($product->getAttributeText('size'));
}
}
$test = new Test();
$test -> run();
Then you can run from console:
php test.php
and it returns in my example
running ..string(11) "Extra Large"
Hope this helps you, next time be more specific.

Relational entity too big to debug [duplicate]

I have around 40 entities and many bidirectional relationships.
Whenever i use var_dump($user) or any entity my browser gets loaded with too much data of arrays and variables then it just crashed.
i want to whats the problem.
The data is being inserted fine. Can i cause issue in production.
Replace var_dump() with the debug method dump() provided by Doctrine Common.
\Doctrine\Common\Util\Debug::dump($user);
It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.
well formatted :
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user, $recurciveLevelToDisplay);
echo '</pre>';
Simple and easy example.
var_dump(serialize($Object));
Symfony < 2.6
You can use \Doctrine\Common\Util\Debug::dump($variable, $depth); it displays doctrine output without the proxy information.
Symfony > 2.6
If you are using symfony 2.6 or more, I strongly advice you to use dump().
It shows a well formated and colored output, and you can dynamically expend/hide rows.
The problem is that in a bidirectional relationship both entities have a link to each other, so while displaying entity1 var_dump will also have to print all properties of entity2, which include entity1 itself giving you a loop.
The get_object_vars() improve the visualization too.
echo "<pre>";
\Doctrine\Common\Util\Debug::dump(get_object_vars($user));
With Symfony 2.6 you can now just use dump($var) in your controller and {{ dump(var) }} in twig.
Make sure to add this to your AppKernal.php file, in the array('dev', 'test') section.
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
use dump($user) and you can see perfect result in Symfony Profiler! good luck
Just use
echo serialize($user);

How to cache data in symfony2

Is there any built in possibility (or an external bundle) to cache data in Symfony2?
I don't want to cache the page itself, but data inside the application, using a simple key -> value store on the file system for example.
There's no built in solution, but I recommend you giving APC, Redis or Memcache a try (they're all in-memory datastores).
You can use LiipDoctrineCacheBundle to integrate cache drivers from Doctrine common into your Symfony project.
i'm using winzouCacheBundle. it gives you a streamlined cache api on different backends (apc,file,memcache,array,xcache, zenddata).
For now, there is no unique solution for caching in Symfony2. Some parts of the framework use Doctrine Common.
There are discussions about a "standard" caching solution if Symfony2, but we will have to wait for some time...
I think the DoctrineCacheBundle is currently the way to go.
The DoctrineCacheBundle allows your Symfony application to use different caching systems through the Doctrine Cache library.
Docs # Symfony.com
Code # Github
If I understand well, you would like to store data (attached to the session) and reload them when the same session will call again a new controller, in order to avoid to execute the same procedure more times (for example to read a table from a database).
You can use the session system in your controllers:
<?php
namespace YourStuff\YourBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class YourController extends Controller
{
$session = $this->get("session");
$variabile = 4;
$session->set("variableName",$variable); // setter
if ($session->has("variableName") // to check if the variable exists
{
$variableName = $session->get("variableName"); // getter
}
}
This is an example; the "variableName" could be accessed next time the same session will be called, if the lifetime of the session is not yet expired.
The "session" uses the __SESSION variable of PHP, so be sure to set correctly the session.cookie_lifetime and session.gc_maxlifetime, in order to give the desired lifetime.

Resources