Rails Sessions

Posted by John
on Sunday, 09 March 2008

rails is love

To make a truly intelligent web application your gonna sooner or later have to play around with Sessions, which is basically a Cookie's big brother.

Store in DB

Now Rails initally will store all it's session data in a text file within the /tmp/sessions directory of your rails app, which is usually ok, but if you're gonna build a production-ready app you're gonna want to up the ante somewhat and store them in your DB.

This is simply done by opening up your environment.rb file and un-commenting the following line...

(within /config/environment.rb) config.actioncontroller.sessionstore = :activerecordstore

Then in the command-line generate a RAKE DB migration for your Session objects (so they're stored in your db from now on).

rake db:sessions:create rake db:migrate

Bingo, you now have a table within your database hooked up and ready to store your session data perfectly.

Storing Session Objects

Now that you've got your DB storing your session data, why don't we start creating some session objects.

This is done by...

session[:order] = 'ASC'

Here, we've simply created a new session object, storing in it a text value of 'ASC'; cool eh?

We can then check to see if our session is empty via...

if session[:order].blank? ..do stuff..

So if our session object contains nothing we can initialise it correctly.

Common Gotcha

Now one of the good things with switching sessions over to your db is that it'll secure your app a little bit better and your performance will improve.

Plus if your building your app on your dev machine, then transfer it to your live box, but can't find a reason why your sessions don't work there it's usually down to file permissions for the session file; by switching sessions over to a db you remove any future problem of this.

Good eh,

Accessing a Network Share with Leopard + Extra

Posted by John
on Sunday, 17 February 2008

Leopard HowTo

First in [Finder], press [command] + K to open the [Connect to Server] box.

Then type your network share's ip address, e.g.

smb://192.168.1.99

Now click [Connect], and you're off.

Belkin Router Problems + Finding the Box

Now recently I got a new ICYBOX IB-N4220-B NAS Enclosure which I now have running as a RAID 1 Array for my own networked WIFI storage, was a bit of a pain with my Belkin router.

Reason being when hooked up there's no real way of testing to see which IP address it's set to on bootup, the supplied CD comes with a tool the find it but that only works on Windows, and didn't work on Vista.

So I hunted around, apparently when it starts up it chooses any IP address in the network starting at 192.168.1.1, and because my Belkin Router had a default setup set to 192.168.2.1 it couldn't find itself.

So setting the Router to 192.168.1.1 meant the NAS drive could then work more happily with the router, picking the next obvious IP down the line, being 192.168.1.2

Finding it's IP then was dead easy, accessed the Router by typing...

http://192.168.1.1

Then logging in, navigating in the menubar, clicking 'DHCP Client List'. From there it listed all the devices currently connected to the Router, one was my MacBook, the other the NAS drive.

So using that I accessed the NAS drive with...

http://192.168.1.2

Logged in and then set it to use a particular IP all the time so I could always find it.

Then used the above commands in Finder to hookup the drive to my MacBook.

Job Done!

Storing My Life Online

Posted by John
on Wednesday, 03 October 2007

So here I am at work, I’ve forgotten my Ipod again (or just haven’t charged it); and I’m sitting here unhappy that I can’t listen to that Foo Fighters track once more.

I’m sure a lot of people have been in this situation, you’ve got a work machine and a home machine and all your stuff is available but only at home and you’re really kicking yourself for not being able to access that data in your lunchbreak. Come on, this is the 21st century and all.

Strongspace

At the beginning of this year Joyent were running one of their deals ‘The Mixed Grill’ plan, that basically offered you a 30GB Shared Server and 25GB secure online storage with StrongSpace for a one-off fee of $499. Well it was the beginning of the month and me being a geek I opted for it thinking it’d take the pain out of monthly fees, also I’m a fan of StrongSpace.

Thankfully this has paid off big time, you cannot imagine how much of a bonus having a development server permanently available to test things out on is; it’s helped me build some FaceBook Apps , practice my PHP code and learn a lot more about Apache.

On top of that the bonus of having 25GB of storage for the lifetime of StrongSpace is such a big bonus, I’ve managed to upload all my important stuff from my personal laptop; development code, etc, up to the service and so far it’s been excellent. I still backup occasionally to DVD but the weight off your mind is titanic!

Music

So with my important stuff on StrongSpace, what do I do about my music ?

Well I’ve looked at a couple of options, Amazon S3 + EC2 may be a choice for future web app hosting. But the one which stands out the most and gives me the ability to stream music from my online account is MediaMax .

You get 25GB upload storage with the free account with a download limit per month of 1GB, sounds good enough for streaming.

With my iTunes collection rolling in at 12GB, 25GB sounds fine so I’ll start uploading that as soon as I can.

My calculations have the total upload of 8,000,000kb with 45kb per second => will take 46 hours to upload so this might take some time. But if I stagger things and just leave stuff on for the initial upload it should take that extra burden off my mind.

Oh no! Legal Blackwater!

However, herein lies a problem; In Japan recently a court ruled that it was illegal to store music files online, also looking more closely at MediaMax and it seems that they’re in a little bit of internal turmoil as they’re switching the technology over to handle new users, so ftp’ing your files has been taken off; real shame.

I don’t know quite how to handle this, do I put my music online in the hope I can access it and that the service doesn’t cut out; and more importantly I don’t get sued?

I dunno, I like Amazon’s policy with regards to the S3 system (they don’t want to know what you store, just how much you use).

All a pretty gray area with regards to online copyrighted music.

So unfortunately till then I think I’ll have to stave off the uploading.

Real shame for the mp3 side, but top dog with my important work.

MySQL 5 + Storage Engines

Posted by John
on Friday, 07 September 2007

MySQL is a very fast and very flexible multi-user / multi-threaded database system used most popularly for Web Applications and offers the simple startup a good platform to build from at very little cost.

Despite other more pedigree Database Systems (SQL Server, Oracle), it offers some fairly advanced functions to the developer and has gained a good reputation for it’s large support base and adherence to standards.

Supporting the ANSI 99 query set, stored prodedures, cursors, triggers, updatable views, text indexing, SSL and even Database Clustering, it’s grown up a bit from it’s relatively meager beginnings.

It can be expanded upon and can quite successfully grow as your company grows with famous users like YouTube , Adobe and Flickr most of which handling millions of transactions within a single hour. Okay the unicode support isn’t completely there, but it’s on the way and release 6 isn’t that far off so watch this space.

MySQL Storage Engines

On top of all this, MySQL offers different ways to handle and manage your data on top of the standard collation types. Allowing you to basically choose an engine that closely fits your company needs.

So if you’re a large archive house that only needs to query old records you can opt for the ARCHIVE engine or if your accessing many remote sources you can choose the FEDERATED engine. The key here is that each one is designed around a specific daily function, allowing you to optimize your hardware for the best performance.

Here’s a look at what’s offered,

MyISAM

(default storage engine, best performance overall)

  • Default install: Yes
  • Data limitations: None
  • Index limitations: 64 indexes per table (32 pre 4.1.2); max 16 columns per index
  • Transaction support: No
  • Locking level: Table

    h4. MERGE

(allows to combine a number of identical tables into one)

  • Data limitations: Underlying tables must be MyISAM
  • Index limitations: N/A
  • Transaction support: No
  • Locking level: Table

    h4. MEMORY

(stores all data in memory, if power failure, you lose it all good for quick access, calculations, rapid temp tables)

  • Data limitations: BLOB and TEXT types not supported
  • Index limitations: None
  • Transaction support: No
  • Locking level: Table

    h4. FEDERATED

(allows remote data access, combining many sources into one system)

  • Data limitations: Limited by remote database
  • Index limitations: N/A
  • Transaction support: No
  • Locking level: No

    h4. ARCHIVE

(insert & select only supported, compressed, good for logs, old data)

  • Data limitations: Data can only be inserted (no updates)
  • Index limitations: N/A
  • Transaction support: No
  • Locking level: N/A

    h4. CSV

(stores as comma-separated data, good for data transport)

  • Data limitations: None
  • Index limitations: Indexing is not supported
  • Transaction support: No
  • Locking level: Table

BLACKHOLE

(allows you to test out possible data structures, schemas)

  • Data limitations: No data is stored, but statements are written to the binary log (and therefore distributed to slave databases)
  • Index limitations: N/A
  • Transaction support: No
  • Locking level: N/A

    h4. ISAM

(original engine, included only for backwards compatibility)

  • Data limitations: Limited maximum database size (4GB)
  • Index limitations: Maximum 16 indexes per table, 16 parts per key
  • Transaction support: No
  • Locking level: Table

    h4. BERKELEY DB

(hash-based storage engine, very quick to access & recover great for accessing data that does not change much, due to it’s table locking)

  • Data limitations: None
  • Index limitations: Max 31 indexes per table, 16 columns per index; max key size 1024 bytes
  • Transaction support: Yes
  • Locking level: Page (8192 bytes)

    h4. INNO DB

(based on myisam + adds database cacheing & indexing, in memory and disk, very fast recovery, less table-locking issues, speeds up recovery & storage. there is a management overhead with InnoDB that requires your system to be optimised to use it but great if you go that extra mile)

  • Data limitations: None
  • Index limitations: None
  • Transaction support: Yes (ACID compliant)
  • Locking level: Row

EXAMPLE

(used as a blueprint to create your own storage engine)

Uses

Having a lot of different storage engines available is a major bonus, but just because I’m nice I’ll let you into what I use. It’s really a mixture but mainly InnoDB (major apps) because it’s less limiting and I like to have total control, but I do have a couple of sites I run for friend’s using the Berkeley DB engine (blogs).

Enjoy,