Memcache++ no comments
Memcache has been a core part of my web programming lately and it’s one of the most flexible and useful systems ever come across.
Think for a second about the process when two people request the same dynamic page on a website. The code rendering one request has no idea that there is another request for the same page so effectively you do the whole process twice. Let’s also imagine that this request pulls a huge list of data out of a massive table that takes a while to query. In an ideal world the dbms should cache the query and the second request should be faster. In reality, and with a simple dbms both sets of data are returned in the same time. Now say one hundred other people also request that same page over the next few minutes, are we going to keep calling that data out of the database from the hard disk or is there a better way?
This is where memcache comes into it’s own. If, after pulling the data out of the database, it is placed in memory and not destroyed after the page is rendered it can be retrieved next time we want the same data. Hey presto, we can now serve thousands more requests per second as we are no longer asking for data from the database, we have exactly what we want waiting in memory.
Memcache isn’t without it’s problems, what if we change what’s in the database. Our version in memcache is now out of date. Thankfully we have set an expiry time on things that we give to memcache, so it falls out of memory after a period of time. What if your data is time sensitive? As with many things I am working on it is important that things happen at certain times rather than just waiting for things to fall out of memory. This is one problem I am currently working on and something I will cover in future.
Haze on the 360! 1 comment
It was only Monday that I was saying how angry I was that we weren’t going to see Haze on the xbox 360. Ubisoft had initially decided it was to be a Playstation 3 title but now it has been announced on the PC and xbox 360 and to be released only a week later! This adds to the already amazing xbxo 360 lineup set for later this year, including Assassins Creed, Bioshock and Halo 3.
UPDATE
I’ve just read that the release schedule was incorrect and the 360 release might not actually be happening. Oh well.
What is this country coming to? 1 comment
A story on bit-tech.net today reveals that Manhunt 2 has been provisionally banned from sale in the UK.
Now I didn’t play the first Manhunt and I don’t really want to play the second but this is a matter of principle we’re talking about here. More and more the regulatory and governmental bodies in the UK are slowly trying to wrap it’s public in a thick layer of bubble-wrap. Even speed limits are being lowered all over the country because some people forgot how to drive safely.
I think my main point is that soon no stupid people will be able to kill themselves and those that have anger issues will no longer be able to vent there frustrations in a video game, their only escape will be to go out into the street and release upon some poor soul (and I’m not talking about water sports).
Rails and AJAX no comments
Rails and rjs templates are the best thing ever!
Do a remote AJAX call from your page and in the linking controller’s view folder have a template called #{action_name}.rjs
In your rjs template you write code in ruby which is translated into javascript which can control your page content. So you can replace html, insert partials and do page effects.
RMagick no comments
I must have spent a good few hours scratching my head when I was trying to get RMagick to work on Mac OS X 10.4.8.
The problem I was encountering was that Ruby would exit with the error: “Bus error”. This nice handy message seems to be plaguing a lot of users.
The problem, as it transpired, was nothing to do with RMagick, but the installation of ImageMagick. I had followed a guide for obtaining all the dependencies for ImageMagick on OSX, but it turns out that there were more, despite ImageMagick compiling and installing without error.
To cut a long story short, the solution was to install ghostscript, something I had blindly missed as one of the dependencies to ImageMagick, and then recompile ImageMagick.
Rails & Keys no comments
Rails was designed with specific names for certain things. Your primary key columns in your database tables are one of these things. Rails expects your primary keys to be called `id`. This is all very well and good until you are building an app with an existing database where the keys are named randomly.
Thankfully there are some clever methods in ActiveRecord to allow you to tell your classes what the tables, primary and foreign keys are. Now comes the annoying part…
If the primary key on your `articles` table is called `articleId` you should be accessing the key using the full name and assigning it with `id`.
@article = Article.find(1) @article.articleId #=> 1 @article = Article.new @article.id = 1
More annoying is when you come to relationships. Say your articles are related to a category, so you’ve set up the relationship in your models and your category table has a primary key named `categoryId`. Seems sensible however now the code you see a lot of people writing won’t work. For example when you’re writing your edit form for your article you’d initially expect to be able to do this.
select("article", "category",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.category)
However since the primary key of category is not `id` the code @article.category will return the object_id and not the primary key. Instead you might expect to do this.
select("article", "category",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.category.categoryId)
Now however you’ve got a whole new bunch of problems. Firstly you can’t use this in a partial that you want to use in your new and edit actions. This is because when you have a new article the category object will be nil, similarly if you are editing a category that isn’t assigned to a category you will get the same error (nil doesn’t have the method categoryId). You might say, you do a conditional, but that introduces more code. You will also have problems when the value is sent back in the parameters, the model will be expecting an object and can’t handle the fact it gets a number.
The best way to do it is to forget the related object and concentrate on your foreign key, we must also change the field name to that of the foreign key in our article table.
select("article", "categoryId",
Category.find(:all).collect {|t| [t.categoryTitle, t.categoryId]},
:selected => @article.categoryId)
This code can now be used in our partial form.
Will it blend? no comments
Who am I?
I’d like to say “I am a young, outgoing, fun person who spends all his time enjoying like to the full”. The reality is that I spend most of my time with my ass firmly planted on my leather^H^H^H^H^H^H^H plastic mock leather chair tapping away at a keyboard.
Why should you read by blog?
I’ll kill you if you stop reading!
Plus… I hope to be able to capture those moments in a web developers life where everything falls apart or on the other hand when everything comes together.
So what am I going to write about?
Writing code, reading code, playing with code and more code. I migiht also add a few other bits in here and there if I get time. I should be writing code you know!
What’s next?
Since I am fairly new to Ruby and Rails I plan to write a bit about why ruby is so great and how rails can be very annoying at the same time as being very useful.