Hi, Just my two cents, but why not just store the link destination, even better, the HTML with the correct re-direction, in a file named according to the hash. You can then just serve it as-is. No lock problem. No database required. O(1) complexity.
Of course this is only valid for a personal, non-public use since that strongly limits the number of links you can have. Also, this is trivially extensible to a pastebin. - syg