1


I have a block in my website that shows the latest 20 items in a database table.
now I need to create an array or object to reside in memory and then to access it for all users who browse my website? Can I do it using PHP?

Thanks for your help

7
  • you have it already. this object is called "query cache" and resides in the DB demon's memory Commented Feb 1, 2011 at 14:01
  • i dont like to access the database!! and as i told you i am going to get the latest 20 items which will be changed every moment. so i want to avoid accessing the DB and make overhead on it Commented Feb 1, 2011 at 14:08
  • 3
    This makes no sense though. Above, Alaa says that it changes "every moment", yet below they say the cache will be updated from the database, presumably "every moment"... so why not just query the database directly, then, and skip the middleman? Unless this is already a huge application with thousands of hits, and the DB overhead is a noticeable drag on things, this smells of premature optimization. Commented Feb 1, 2011 at 14:20
  • 2
    100k a day is (confined to an eight-hour work day) about 8 a second; I would posit that's not a humongous amount, but I'm no expert on large applications. I again suggest though that before you move to memcache or APC, you perform metrics and determine that it isn't actually DB overhead causing any delays (you haven't stated if you did, or if there are actually any delays, so we're running only off the small information you've shared). Moral of the story: Don't assume there's overhead, test for it first. If you have, then great, but you haven't revealed that fact. Commented Feb 1, 2011 at 14:35
  • 1
    Addendum to above: 100k for an eight hour day is actually only three hits a second, which I would think is well within the means of any system to handle without fancy caching. Commented Feb 1, 2011 at 14:39

3 Answers 3

2

Look at options like APC or memcache, or WinCache if you're on a Windows server. These all provide options to cache data/objects.

If this is to show the latest items on a database, you'd need to update it every time something is added to that database, otherwise it won't be consistent with the database

Sign up to request clarification or add additional context in comments.

1 Comment

sure i will update it,,, I am using drupal in my website and thus i will update this object using nodeapi upon insertion time By the way... my server is linux CENTOS
1


Thanks for your contributions.
i found a way to do it.
The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide framework for caching.
try this code and refresh the page or open it using different browsers

<?php 
if (apc_exists('test'))
     echo apc_fetch('test');
else{
   echo "Just Created ";
   apc_store("test",time(),6666);
   echo  apc_fetch('test');
}
?>

Comments

0

PHP is stateless so it is not possible to easily to share the variable between the different session. You can do this by one trick. Create array of all these value, Now serialize the array $sharedObj = serialize ($originalArray) ; you can get Serialize variable of your $originalArray in $sharedObject Write the $sharedObject into a text file and you can read the text file and unserialize that data(which you are getting from the text file) and you can get the same array.

Please let me know if you still have problem.

5 Comments

The question now being, does the filesystem have more overhead than the DB?
@Andrew: Indeed - before we went to memcached, we used a small ramzdisk for this - precisely because disks were too slow. That was a "10k+ simultaneous users" kind of webapp, though.
That doesn't sound like a good idea. I would think both DB queries and memcached would be faster than repeatedly 1) writing and 2) reading the filesystem.
OMG, XML file. a cure that worst than disease.
@Alaa: The overhead of serializing and deserializing XML, together with disk I/O bottlenecks, will likely be greater than the speed improvement gained from avoiding the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.