Home > Blog > 2013 > 10

All Posts From : October 2013

Using Route Hijacking to Enable Page Caching in Umbraco

Back in the days of Web Forms, there were two main ways of implementing caching in Umbraco. You could cache the parts of the pages that were macros using the Umbraco marco caching features, or you could use the built in ASP.Net control and page level caching features (which perform slightly better than the Umbraco ones).

Now that Umbraco support MVC views, you no longer have the option to specify page level caching in your page templates like you could in the header of your web forms master templates, where you could add something like this to your master pages:

<%@ OutputCache Duration="60" VaryByParam="None"%>

You can't specify caching within a view itself, you either have to use the caching when you render a partial macro,  which you can do using Umbraco or you have to use an OutputCache directive on your action controllers.

Recently I worked on a site in Umbraco 6 where there were about five or six news/blog sections, each with their own RSS feeds, and then there was a master RSS feed in the root of the site that aggregated them all. The RSS feeds were pretty popular, so I really wanted to cache them for around 60 seconds, to help boost performance.

Using Umbraco partial caching, I'd have had to create a partial to render the RSS, and then had an "outer" view that loaded the partial and applied the caching, which I wasn't keen on, I'd rather cache the whole thing. Enter route hijacking in Umbraco. What is route hijacking? It gives you the ability to create your own controllers that will handle DocTypes and also Templates in a custom manner. As these allow you to override the ActionResult, that means you can apply an output caching directive to those templates.

So, in my example site, I have a Document Type called "RssFeed" that I would like to implement caching on for the WHOLE template. How would I do this? It's REALLY easy.

In my extensions project for my site (which contains all of my custom models, logic and helper methods), I added a class called "RssFeedController" with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Umbraco.Web.Models;
namespace MySite.UmbracoExtensions.Controllers { public class RssFeedController : Umbraco.Web.Mvc.RenderMvcController { [OutputCache(Duration = 60)] public ActionResult Index(RenderModel model) { return base.Index(model); } } }

Note, that the name of the controller is very important, it should be the name of the DocType, with all spaces etc removed, followed by the word "Controller". So if your page was called "Awesome Feed" the class name would be "AwesomeFeedController". Notice the OutputCache declaration, all I'm doing is spcifying that the output should be cached for 60 seconds. There's no need to vary the cache, as the feeds can't be altered by the querystring or by user etc.

Compile the code and add the DLL to your site and it should be up and running! All Your RSS feeds are now cached for 60 seconds, and the best part is, they're cached by path, so each of the separate feeds is cached independantly.

This is only a very basic example, you can do a lot more with this, like vary the cache by parameters, specify whether the content should be cached locally or on the server, and even set different options for other allowed templates for the same DocType.

You can find out more about route hijacking  in the Umbraco docs, and more detailed information on how output caching works in MVC  on the MSDN. I hope this proves useful to someone :)

Tips For Aspiring Freelancers

This year I'll have been freelancing for 7 years. Prior to that I worked at agencies for 8 years. I get to work with all sorts of people, and something I often get asked is "what's the secret to being a freelancer?". Honestly, it's just hard work. Being a freelancer can be harder work than some peple think, but if you're prepared to put in the hours, it can be hugely rewarding. I haven't looked back since I quit my last full time job.

Here are some useful bits of info that I've picked up over the years that have helped me (not all of them are things I've done, some are things I've come across in my travels).

1. Love what you do

Sounds obvious, but if you don't have a passion for web dev, you'll never progress very far. This industry is VERY fast paced compared to others, you need to keep on top of current trends and technologies. Doing that is much easier if you actually enjoy reading up on cool new CSS tricks and server side technologies. It'll also come across when you're talking to clients, enthusiuasm is infectious!

2. Open source tools are your friend

Regardless of whether you're a PHP, ASP.NET, Rails or even Coldfusion kind of person, open source software and frameworks can be invaluable. I use a lot of open source tools for my work and they save me a lot of time during development. Make CMS websites? Find an open source solution that floats your boat, and use it. I switched from home brewed CMS systems to Umbraco (and occasionally a few other systems) a few years back, and I haven't looked back. You'll spend less time wrinting tedious CRUD code, and more time making the website work EXACTLY how the client wants. Tools like Grunt can be invaluable time savers during dev as well.

3. Focus Up

As a freelancer, you need to be able to focus on work, especially if you work from home. Make sure your work area is free from distractions. If you keep wondering off to play on the Xbox, or hopping on to Warcraft, you're not going to get much work done. I have a separate room at home for work, so I can concentrate without interruptions.

4. Learn to say no

Early in your career, you'll be tempted to say yes to everything that gets thrown your way. Unless the wolves are literally massing at your door, don't. Some jobs just aren't worth it. If a client/job looks like it's going to be a nightmare, consider saying no. Be polite and professional about it. Warning signs include things like clients changing their mind every 30 seconds, poorly defined specifications, clients constantly trying to get extra functionality for free, ANYONE who asks you to build Amazon in a few weeks and my own personal favourite "I can't afford to pay you, but you can have a share of the awesome profits".

5. Remember the rainy days

Unless you're lucky enough to have a long term regular contract, there will be periods where you aren't working. Don't forget to save for those occasions. Also, don't forget that you don't get sick pay, a lesson I learned the hard way when I was diagnosed with Cancer a few months after going freelance!

6. Get organised

If you've ever seen the first episode of Black Books where Bernard does his tax return, you'll know what I'm talking about here. Organise your freelance work properly and you'll make your life so much easier. Keep track of expenses and payments, otherwise, come tax return time, you'll be running round desperately trying to find receipts and you'll end up not claiming back expenses you should have. The same applies to your work load. Make sure your work schedule is well organised and you won't end up overbooking yourself and pulling 20 hour+ days to meet all of your commitments.

7. Code nicely

There's a saying that you should always write your code as if the next person who will work with it is a 7ft angry psychopath who knows where you live. It's true too. NO ONE likes having to work on crappy code, and it doesn't take much effort to format your code properly and comment it if needed. I've seen freelancers (and inhouse guys) who go out of their way to write overly complex code out of the mistaken belief it will make them somehow indespensible. It won't. Other freelancers and your client's inhouse guys will hate you forever, and that means you're less likely to get more work if word gets out you write awful code.

8. Give credit where it's due

As a freelancer, you are often treated better than inhouse guys when it comes to voicing ideas. You can say something that the inhouse team has been saying for years, and the higher ups will listen to you, because you're expensive (I've always found this stupid, but it's happened a few times in my career). If that happens, be sure to make sure the inhouse guys get all the credit. Otherwise you'll get a reputation for being a dick who steals credit/ideas.

9. Share your toys

Don't be precious about your code. If you write something cool, share it with the wider developer community. It's scary sharing your code for the first time, but it can be very rewarding. You'll make new friends and contacts, and in some cases you may even get work out of it. I participate in the awesome Umbraco community and find it very rewarding and dare I say, fun! If the inhouse guys at your clients want to know how stuff works, show them! I got started in the industry picking the brains of a ASP/VBScript freelancer back in the day. He helped me to get into server side programming and the rest is history! People will remember you if you're friendly and helpful and are more likely to recommend you to others. 

10. Approach agencies directly

It's OK to use recruitment agencies, but they charge 25%+ on top of your hourly rates to companies, which makes them expensive. If, like me, you work with design agencies, approach them directly about freelance work. You might not get anything out of it straight away, but by going to them directly, you can sell yourself better than a recruitment agency probably will, and if you get on, you may get work out of it. From a design agency point of view a freelancer they employ directly and have at least spoken to is probably cheaper/better than some random schmo that a recuiter sends over.

11. Don't be a dick

This was sort of covered in #8, but be nice. Sweeping in to a client's office, telling them that all their inhouse guys are idiots and that you are the only one that can sort out their problems makes you look like a total arsehat. People won't like you much, and when they leave, they won't be recommending you when their new boss asks if they know any good freelancers.

12. You WILL end up doing a lot of crappy jobs

Early in my career I got brought into agencies a lot to fix projects that other freelancers had messed up, bailed on, or otherwise not done properly. I won't lie, after the first couple, it gets pretty soul destroying. I had a period of about two years where about 60% of my time was spent sorting out problems caused by the same two or three guys. Get used to it, and use it as a learning tool. I learnt as much about how NOT to do things from those years as I did about coding. I also learnt a LOT about debugging and troubleshooting problematic code. Stick it out, and as clients come to know and trust you, you'll get better work!

13. Chasing payments

This is the really unglamorous part. When you work full time, money just appears in your bank account every month, yay! As a freelancer you have to deal with clients that are crappy payers. They pay late, sometimes by many months, or try to weasel out of paying for stuff. Dedicate at LEAST a day a month to chasing down payments that are overdue, and make sure you get paid. In the UK, you're entitled to charge interest on overdue invoices, so make sure you mention that on your invoices/contracts (also, be VERY clear about your terms of payment, e.g. 30 days). It's surprising how quickly people will pay up when you start issueing invoices with interest on. I also have a very small blacklist of places I won't work for any more, as they've been so bad with payments, I'd rather not waste my time with them again.

14. Learn new things

The industry changes very rapidly, as discussed in #1. As a freelancer, you aren't going to get sent on training courses to help develop your career. If you want to learn stuff, you have to do it yourself. Even if you aren't necessarily going to use new technologies, it's worth at least looking into them, so that you can consider them for your projects. Try and spend a few hours a week investigating what's hot in your field of expertise. Don't be one of those guys that still codes in ASP/VBScript because that's "what you know" and never move on. Otherwise you just may find yourself at the point where your skills are all totally useless (that said, I STILL occasionally get ASP/VBScript work, believe it or not).

15. Network!!!

As a freelancer, you're only going to get hired if people know who you are! Go to conferences, if there are local developer meetups try and go so you can meet new folks and network. If you're super ambitious, write a blog and promote the crap out of it, talk at conferences, hell, write a book. It all helps to get your name out there so that people will consider hiring you. I do occasionally go through recuitment agencies, but I prefer to work with clients directly, as you can build up a better relationship that way. Also, I cannot stress enough how important it is to network with account handlers and project managers. They move around a lot more than us techy types do, and if they've worked with you and they liked you, there's a chnace they'll recommend you at their next job!

That'll do for the moment! I hope this proves useful to someone :) I may end up expanding on this at some point in the future if there's any interest.