Using memcache and Zend_Cache

Probably one of the more useful things you can do with memcache is drop it into Zend Framework. Here’s how I did that.

Following on from the previous post, here is an example of how you can set up memcache to serve as a cache for your Zend project.

I like to use the method style of bootstrapping my application, so here’s the function that builds the cache for me.

Originally, I was getting the “Memcache::addServer function expects at most 6 parameters, 8 given” warning. To force Zend to play nice with memcache (and send the right parameter count), you need to make sure you have the compatibility option set to true in the backend options array.

Here’s an example on how to use:

Hope this helps in getting started with Zend_Cache (even on a technology as old as memcache).

Enabling memcache under MAMP

I know the tech is a bit old, but sometimes you need to make do with the tools you’ve been given. Here’s how I enabled memcache under MAMP.

First, grab the precompiled binary file (memcache.so) from here. There were no problems using this on Snow Leopard (for me, at least).

Second, copy the file to your MAMP extensions directory. You should be able to find it here: /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-[date].

Next, you’ll need to enable the extension in MAMP. Switch it off, then open up php5.ini by going to File > Edit Template > PHP5 php.ini

Find the extensions section (about line 540 or so) and add this line to the list of extensions:

Once that’s done, save php.ini and reboot MAMP. You should be able to see an entry for memcache when opening your phpinfo page.

“Boyfriend went Vegan”

Here’s one of many new ads aimed at promoting veganism (a noble goal, I’m sure) by our good friends over at PETA.

A HA HAHA HAHA HA HA HA

Wait. This isn’t funny or clever, it’s an abomination. If I were vegan, I’d pretend that I wasn’t until this atrocious “marketing” campaign passed and we all walked around like it never happened.

“Remember those ads by PETA?”
“Wha- what? HELL NO. Let’s go eat some cabbage.”
“Rock on, pasty friend.”

Yeah, just like that.

Vegans get all the breaks
The battered wife look is so hot right now

Reclaiming your favicons when using MAMP

I use MAMP Pro to manage my local virtual hosts. It’s about a hundred times more convenient than Apple’s “native” Apache/PHP install (unfortunately a sad truth).

One annoying facet, though, is that MAMP will force its favicon when you’re using a MAMP-managed virtual host. Here’s how to get rid of this “feature” and reclaim your presumably awesome favicon.

First, make sure MAMP is not running Apache. Then, go to File > Edit Template > Apache httpd.conf

Once you’ve got the configuration file open, look for the line (roughly 625 for me):

Alias /favicon.ico "/Applications/MAMP/bin/favicon.ico"

and comment it out with a hash (#). Save your configuration, then reboot MAMP’s Apache server. You might have to flush your browser’s cache before it recognises your own favicons.

This took way too long to fix, so I hope this helped someone else.

Simple Remote Requests with Titanium Appcelerator

Titanium Appcelerator is a pretty nifty tool. Here’s a small data manager object that looked after my external requests.

I was tinkering with Appcelerator recently, and came upon the need to build a data manager to shoot off requests in a bit of a “fire-and-forget” sort of way. I created this global object I could channel requests into. My particular server always replied with JSON, so you can tweak this for whatever data your remote server will return.

Using this object in the global scope, you can just type:

Appcelerator has sped up development in key areas tremendously, but at the cost of some flexibility and code bloat. It’s worth keeping an eye on, as each release is leaps and bounds ahead of its predecessor.

Accessing local sites on VirtualBox

One of the most frustrating things about setting up VirtualBox for the first time is setting up a local host. It isn’t immediately obvious, and finding documentation was difficult. When you know how to get it running, it’s easy.

Here’s how:

1. Set up your VirtualBox image as normal.

2. On the networking tab, make sure you have the primary network adapter set to NAT

3. Boot your VirtualBox image and open your ‘hosts’ file using Notepad. This can be found at C:\Windows\System32\drivers\etc

4. Add your host entry as it is found on your main machine. In this example, I’m using the my site’s development copy ‘timneill.local’. See below.

5. Save the hosts file and enter your address into the browser. You should see your locally served site.

This was tested using Mac OS X and VirtualBox running WinXP. There is no need to flush the DNS cache for Windows after WinME.

If there are any problems, leave a comment or send me an email.

iRiver Story and Mac OS X

I acquired an iRiver Story recently, and couldn’t figure out how to mount the disk on my Mac OS X machine. The device didn’t automount, and it didn’t show up in the attached devices list.

A bit of scrounging led me to the solution:

  1. First, make sure the iRiver is plugged in.
  2. Power the device on, and wait for it to boot.
  3. When the prompt appears for charge or USB connection, choose USB connection.

The key thing to note here (and it was the problem I ran into) is that the prompt appears for about three seconds, which is barely enough time to figure out what you want to do with it. If you miss the prompt, simply unplug and re-plug the device.

© iRiver

As for document management… you can just drag and drop your files into the directories shown in Finder. You can (if you like) grab something like Adobe’s Digital Editions, but at the end of the day you don’t really need something so bloated to manage a handful of digital books.

Watching your Language

Inspired somewhat by Dave Ray’s post “Say Something Nice About Every Language You’ve Used“, I’ve decided to do the same.

PHP
Fairly easy to learn, and with an improving OO model, Web development will only get better. Syntax is pretty straight forward and it’s easy to get something working relatively quickly.

Visual Basic
Very simple syntax, and a good introductory language for learning Windows programming.

C/C++
I like C (and C++) because you can do with it what you will. There’s a lot of flexibility and room to move for whatever problem you have.

Java
It has a similar syntax to C, but automatic garbage collection makes me weep tears of joy.

Javascript
One of my favourite aspects of web development is putting on my Javascript hat and adding very slick functionality to my site. Simple syntax and a lot of power for overriding default objects.

Objective-C
Very quick to learn the basic syntax, and it forces a separation of code into conceptual pieces that I agree with.

That exercise is actually enlightening. I think focussing on the strengths of a particular language allows you to leverage it for maximum effectiveness. I’m fairly sure I’ve picked up more languages than this in the past, but not with enough experience for any sort of nice opinion. Perhaps next time I’ll just bitch and moan about all the languages I’ve used.

Return JSON data with Zend Framework

Getting Zend to return JSON data can be tricky at times. The JSON helper likes to send the ‘official’ JSON header (application/json), and most browsers are treating that as a download link, resulting in no data to the client and an unfortunate download box.

To bypass this default behaviour, you’ll need to tell the controller how to serve your JSON data:

The key part of this stub is the exit; line. It prevents the view renderer from sending anything, keeping your JSON data intact. Note: json_encode and json_decode only works from PHP version 5.2.1

To output all of your view’s variables in this way, replace $some_var; with:

Hopefully this saved someone a headache or two when dealing with Zend’s puzzling JSON implementation.

Modal View Controller Example – Part 2

In the first part of this tutorial, we set up a pair of simple views in Interface Builder that we switched between modally. In this tutorial, we’ll make them somewhat useful and pass data between them using delegates.

The concept of protocols and delegates is an important and somewhat complex one, but I like to think of it in these simplified terms:

Basically, the object that implements our protocol agrees to implement the methods of that protocol. In the case of this tutorial, we’ll be connecting the modal view with our main view using a delegate.

Firstly, we’ll create the interface elements for our project. Open the ModalViewExampleViewController XIB file and create a button and a label as shown.

Next, add those interface elements to ModalViewExampleViewController.h. We’re also adding the necessary IBAction also:

Be sure to include the necessary additions to ModalViewExampleViewController.m:

Jump to Interface Builder and be sure to link the new elements with the properties we defined. Refer to Part 1 of this tutorial for a guide on how to do that.

The next step is key. We will create a basic protocol and then assign a delegate. Open up ModalViewExampleViewController.h and add this:

We then tell ModalViewExampleViewController to implement this protocol:

We also need to add the protocol’s method to the main implementation:

Once we have these in place, the next step is to set up a reference between the two views. What we will do is define a delegate inside of SampleView so that we can send messages to it.
Include the protocol in ModalViewExampleViewController.h and add the reference:

Be sure to synthesize the delegate in SampleViewController.m, and include the necessary header files.

So far we have defined a protocol inside of our parent view, and defined a delegate in our modal view. The next step is to link them together and make them useful.
Firstly, we’ll write the functions that will handle the messages. Replace the original definition of didReceiveMessage with this:

And also add the following code to showWithDelegate:

What we’ve done is create a SampleView object and assigned its delegate to be the parent view. That is, the parent view will be handling messages sent by SampleView.
Open up SampleViewController.m and add the code to send the message.

Compile the app and run it. You should be able to see the text “Hello World” passed from one view to another once you dismiss your modal view with delegate. You can extend this any way you like with additional controls on the modal view, such as sliders or text input.

Download the source code for this project here.