8
I Use This!
Inactive

News

Analyzed about 24 hours ago. based on code collected 2 days ago.
Posted almost 16 years ago by Brian Moon
MemProxy 0.1 is out!  It has taken me a while, but I have finally gotten around to releasing the code that I credited with saving us during a Yahoo! mention.  It is a caching proxy “server” that uses memcached for storing the cache.  I put server in quotes because it is really just a PHP […]
Posted almost 16 years ago by Brian Moon
MemProxy 0.1 is out!  It has taken me a while, but I have finally gotten around to releasing the code that I credited with saving us during a Yahoo! mention.  It is a caching proxy “server” that uses memcached for storing the cache.  I put server in quotes because it is really just a PHP […]
Posted almost 16 years ago by brianlmoon
MemProxy 0.1 is out!  It has taken me a while, but I have finally gotten around to releasing the code that I credited with saving us during a Yahoo! mention.  It is a caching proxy "server" that uses memcached for storing the cache.  I put server in ... [More] quotes because it is really just a PHP script that handles the caching and talking to the application servers.  Apache and other HTTP servers already do a good job talking HTTP to a vast myriad of clients.  I did not see any reason to reinvent the wheel.  Here are some of the features that make it different from anything I could find: Uses memcached for storage Serves cache headers to clients based on TTL of cached data Uses custom headers to assemble multiple pieces of cache into one object Minimal dependencies.  Only PHP and pecl/memcached needed. Small code base.  It is just two files, one when settings are cached. Application agnostic.  If the backend is hosted on an HTTP server this can cache it. Some other things it does that you might expect: Handles HTTP 1.1 requests to the backend Allows TTLs set by the standard Cache-Control header Appears transparent to the client. Sends proper HTTP error codes relating to proxies/gateways Allows pages to be refreshed or removed from cache Allows a page to be viewed from the application server without caching it more.... You can find the code on Google Code.  The code (or something like it rather) has been in use at dealnews for well over a year.  But, this is a new code base.  It had to be refactored for public consumption.  So, there may be bugs. [Less]
Posted almost 16 years ago by brianlmoon
So, just now, I was reading a good Rails post by Stuart Herbert and nodding my head along.  I have not gotten into the Rails bashing fun on my blog, but I do poke fun at it around the office.  Then I got to this part: The OO in Rails continues to ... [More] leave PHP for dead, and OO brings many advantages to a thriving development community.  There are real advantages to being able to share code between both the must-be-real-time web front-end and the non-real time backends, and to be able to easily reuse whatever external open-source libraries save you time and effort. Now, I have no idea about the first part.  I am not an OOP guy.  But, what I have issue with is the idea that for code to be reusable, it has to be OOP.  So, if I am a college kid or young PHP developer, I would read this and think "Oh, so, to reuse code or share it, I have to be using OOP".  Man, this is just so dead wrong and irresponsible.  Can someone tell me why only OOP can be reused?  Why can't people write sane functions that can be reused?  I do it every day.  They do it in C all the time.  Our front end web servers run the same code base as the cron jobs that do a wide variety of things.  They use the same libraries.  They use the same objects (yeah, i use them when they are a good idea). Please, someone explain this to me. (I have a half written post about how you can write good, maintainable, reusable code without OOP.  I have not finished it yet, but I guess I need to.  It seems the world is going to OOP hell otherwise.) [Less]
Posted almost 16 years ago by Brian Moon
So, we had a cron job hanging for hours.  No idea why.  So, I started debugging.  It all came down to a call to in_array().  See, this job is importing data from a huge XML file into MySQL.  After it is done, we want to compare the data we just added/updated to the data in […]
Posted almost 16 years ago by Brian Moon
So, we had a cron job hanging for hours.  No idea why.  So, I started debugging.  It all came down to a call to in_array().  See, this job is importing data from a huge XML file into MySQL.  After it is done, we want to compare the data we just added/updated to the data in […]
Posted almost 16 years ago by Brian Moon
So, we had a cron job hanging for hours.  No idea why.  So, I started debugging.  It all came down to a call to in_array().  See, this job is importing data from a huge XML file into MySQL.  After it is done, we want to compare the data we just added/updated to the data in [...]
Posted almost 16 years ago by brianlmoon
So, we had a cron job hanging for hours.  No idea why.  So, I started debugging.  It all came down to a call to in_array().  See, this job is importing data from a huge XML file into MySQL.  After it is done, we want to compare the data we just ... [More] added/updated to the data in the table so we can deactivate any data we did not update.  We were using a mod_time field in mysql in the past.  But, that proved to be an issue when we wanted to start skipping rows from the XML that were present but unchanged.  Doing that saved a lot of MySQL writes and sped up the process. So, anyhow, we have this huge array of ids accumulated during the import.  So, an in clause with 2 million parts would suck.  So, we suck back all the ids in the database that exist and stick that into an array.  We then compared the two arrays by looping one array and using in_array() to check if the value was in the second array.  Here is a pseudo example that shows the idea: [sourcecode language='php'] foreach($arr1 as $key=>$i){ if(in_array($i, $arr2)){ unset($arr1[$key]); } } [/sourcecode] So, that was running for hours with about 400k items.  Our data did not contain the value as the key, but it could as the value was unique.  So, I added it.  So, now, the code looks like: [sourcecode language='php'] foreach($arr1 as $key=>$i){ if(isset($arr2[$i])){ unset($arr1[$key]); } } [/sourcecode] Yeah, that runs in .8 seconds.  Much better. So, why were we using in_array to start with if in_array is clearly not the right solution to this problem?  Well, it was basic code evolution.  Originally, these imports would be maybe 100 items.  But, things changed. FWIW,  I tried array_diff() as well.  It took 25 seconds.  Way better than looping and calling in_array, but still not as quick as a simple isset check.  There was refactoring needed to put the values into the keys of the array. UPDATE: I updated this post to properly reflect that there is nothing wrong with in_array, but simply that it was not the right solution to this problem.  I wrote this late and did not properly express this.  Thanks to all those people in the comments that helped explain this. [Less]
Posted almost 16 years ago by brianlmoon
SimpleXML is neat.  Some people don't think it is so simple.  Boy, use the old stuff.  The DOM-XML stuff. Anyhow, one annoying thing about SimpleXML has to do with caching.  When using web services, we often cache the contents we get back.  We were ... [More] having a problem where we would get an error about a SimpleXML node not existing.  We were caching the data in memcached which serializes the variable.  So, when it unserialized the variable, there were references in there to some SimpleXML nodes that we did not take care of.  Basically, a tag like: bar is a string.  But a tag like: is an empty SimpleXML Object.  That is a little annoying, but I don't feel like digging into the C code and figuring out why.  So, we just work around it.  We made a recursive function to do the dirty work for us. function makeArray($obj) { $arr = (array)$obj; if(empty($arr)){ $arr = ""; } else { foreach($arr as $key=>$value){ if(!is_scalar($value)){ $arr[$key] = makeArray($value); } } } return $arr; } That will turn whatever you pass it into an array or empty string if it is empty. But, while I was hacking around tonight, I came up with another idea.  Check out this hackery: $data = json_decode(json_encode($data)); Yeah!  One liner.  That converts all the SimpleXML elements into stdClass objects.  All other vars are left intact. Ok, so this is where someone in the comments can tell me about the magic SimpleXML method or magic OOP function I have missed to take care of all this.  Go ahead, please make my code faster.  I dare you. [Less]
Posted almost 16 years ago by brianlmoon
We had a huge hail storm today. It came out of nowhere.  It was kind of scary. Here is a photo that may yield more perspective.