HODGEDEV FLASH BLOG

CachedSharedObject Class, A Local Shared Object that clears with the browser cache.

by Brian Hodge on Apr.01, 2009, under Actionscript 3.0

DEMO: http://dev.hodgedev.com/DeSOL_testing/

Note: This is not intended as a replacement for SharedObject. Obviously if you still require some level of persistence you could just use the native SharedObject; However, if you require the use of a SharedObject and would like it to clear with the browser cache, or by a certain date, this class can help you.

The class works by creating a cookie when the shared object is created; This is done with the help of the ExternalInterface Class in the flash.external Package which makes a call to a javascript function.

From the point of creation, the SharedObjects lifespan is directly related to that of the Cookie’s life span. The two share the same name, and upon load of the page a function is called to check if a particular cookie exists. The cookie acts as a reference for the SharedObject. If the cookie is removed, meaning the cache has been cleared, the next page load checks the cookie, finds that it does not exists, and clears the SharedObject that has the same name.

  1.  
  2. //The AS3 Class
  3. package com.hodgedev.desol.net
  4. {
  5. import flash.external.ExternalInterface;
  6. import flash.net.SharedObject;
  7. import flash.net.SharedObjectFlushStatus;
  8. /**
  9. * …
  10. * @author Brian Hodge (brian@hodgedev.com)
  11. */
  12. public class CachedSharedObject extends SharedObject
  13. {
  14. /**
  15. * creates a cookie via ExternalInterface and a call to a
  16. * javascript function which is expecting three things:
  17. * a name, a value, how many days until expiration. A
  18. * SharedObject and a cookie are created with the same name.
  19. * The SharedObjects life is dependent up on cookie's life.
  20. * @param pName string used to name the SharedObject
  21. * @return SharedObject
  22. */
  23. public static function getLocalCSO(pName:String):SharedObject
  24. {
  25. var createStr:String = ExternalInterface.call("createCookie", pName, "Exists to allow flash local shared objects to clear with browser cache clear.", "7 " );
  26. return SharedObject.getLocal(pName);
  27. }
  28. /**
  29. * an array of cookies recieved from the javascript window.cookies
  30. * call returned as a string then converted to an array.
  31. * @return
  32. */
  33. public static function cookies():Array
  34. {
  35. return String(ExternalInterface.call("getCookies")).split(";");
  36. }
  37. /**
  38. * Acts as the garbage collection for a CachedSharedObject.
  39. * The cookie that was created at the same time is the
  40. * reference. If the reference no longer exists, hence
  41. * the cache was cleared, the SharedObject is removed.
  42. * @return Boolean
  43. */
  44. public static function LSOGC():Boolean
  45. {
  46. if (CachedSharedObject.cookies().length < = 1 )
  47. {
  48. trace('No cookies found, clearing all CachedSharedObjects.');
  49. return true;
  50. }
  51. return false;
  52. }
  53. /**
  54. * reads a cookie via ExternalInterface. A
  55. * javascript function returns a string which
  56. * this function then returns a reformmated
  57. * version of.
  58. * @param pName
  59. * @return String
  60. */
  61. public static function readCookie(pName:String):String
  62. {
  63. var readStr:String = ExternalInterface.call("readCookie", pName);
  64. return "Cookie name: " + pName + "\nCookie value: " + readStr;
  65. }
  66. /**
  67. * Erases a cookia via ExternalInterface. A
  68. * javascript function deletes the cookie.
  69. * @param pName
  70. */
  71. public static function eraseCookie(pName:String):void
  72. {
  73. ExternalInterface.call("eraseCookie", pName);
  74. }
  75. }
  76. }
  77. //The javascript
  78. /**
  79.  *Author URL: http://www.quirksmode.org/js/cookies.html
  80.  *Notes: Thanks to the author at the above site for the
  81.  *easy to use functions.
  82.  */
  83. function getCookies()
  84. {
  85. return unescape(document.cookie.toString());
  86. }
  87. function createCookie(name,value,days)
  88. {
  89. if (days)
  90. {
  91. var date = new Date();
  92. date.setTime(date.getTime()+(days*24*60*60*1000));
  93. var expires = "; expires="+date.toGMTString();
  94. }
  95. else var expires = "";
  96. document.cookie = name+"="+value+expires+"; path=/";
  97. return "Cookie created.";
  98. }
  99. function readCookie(name)
  100. {
  101. var nameEQ = name + "=";
  102. var ca = document.cookie.split(';');
  103. for(var i=0;i < ca.length;i++)
  104. {
  105. var c = ca[i];
  106. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  107. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  108. }
  109. return null;
  110. }
  111. function eraseCookie(name)
  112. {
  113. createCookie(name,"",-1);
  114. }

Download the source files here.

:

1 Comment for this entry

  • Lellacype

    Great site this blog.hodgedev.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor :)

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...