Using LocalSharedObjects to cache user’s preferences in ActionScript 3

At Vokle, I store a decent amount of information about you using LocalSharedObjects or “Flash cookies”. What kind of information you might ask? Nothing personally identifiable, that gets tossed in regular cookies along with peanut butter M&Ms. I store your preferred camera and microphone, whether or not you’ve seen any “first time” information for setting up your computer and your audio/video quality settings. It’s nice to have a consistent interface for saving and retrieving all that information and I’m including a simple example below.

Problem

You want to cache information about a user’s system and retrieve it later quickly and easily.

Solution

UserSettings is a singleton class because chances are you’re not going to need multiple instances of it floating around your application. My preferred singleton way of enforcing singletons (since it isn’t officially supported) in ActionScript 3 is detailed here if you’re interested. In your client code you would do UserSettings.instance.setValue()/getValue().

UserSettings.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package utils {
 
	import flash.errors.IllegalOperationError;
	import flash.net.SharedObject;
 
	/**
	 * This class loads the application's Local RSO for a particular computer/browser.
         * Use it to store and retrieve values that make sense to save specifically about this computer such as the user's preferred microphone or camera.
	 */
	public class UserSettings{
 
		/**
		 * The Singleton instance of the Settings to use when accessing properties and methods of the Settings.
		 */		
		public static const instance:UserSettings = new UserSettings( SettingsSingletonLock );
 
		private var _settings_RSO:SharedObject	= null;
 
		/**
		 * DO NOT CALL THE CONSTRUCTOR DIRECTLY, INSTEAD USE Settings.instance
		 */
		public function UserSettings( lock:Class ){
			if( lock != SettingsSingletonLock ){
				throw new IllegalOperationError( "Settings is a Singleton, please use the static instance method instead." );
			}else{
				_settings_RSO = SharedObject.getLocal( "settings", "/" );
			}
		}
 
		/**
		 * Retrieves a value in the settings by the specified key.
		 * 
		 * @param key	(Object) the key that the desired setting data was saved under
		 * @return 		(Object) returns the data saved by key
		 */
		public function getValue( key:Object ):Object {
			return _settings_RSO.data[key];
		}
 
		/**
		 * Saves a value into the settings under the specified key.
		 * 
		 * @param key	(String) the key under which to save the value
		 * @param value (Object) the value to be saved
		 * 
		 * @return		(Boolean) true if the value was successfully saved
		 */
		public function setValue( key:String, value:Object ):Boolean {
			_settings_RSO.setProperty( key, value );
			return _settings_RSO.data[key] == value;
		}
 
	}
}
 
class SettingsSingletonLock {}

Gotchas

These values are obviously saved per computer just like a normal cookie and are not shared between IE and other browsers on PC. This means you should be storing information related to the user’s system or anything that wouldn’t be essential to have already should the user decide to use a different computer. Also, you should never be storing any personally identifiable or sensitive information in LocalSharedObjects just like you shouldn’t in normal browser cookies.

Of course, you can add your own custom getters/setters if you want strict type checking for certain values or just to save time, I do. If you have non-advertising related sneaky uses for LocalSharedObjects or have a similar class of your own, write it up and let me know in the comments!

Tagged : , , ,

Leave a Reply