public
Description: Flying Saucer as3 helper classes
Clone URL: git://github.com/csessions/as3.git
Auto flush Cache on write, but no more than once per second
csessions (author)
Thu Aug 21 23:35:25 -0700 2008
commit  5d87bcbb0ce50f5e7c8aa331d74b4d26dd9628e7
tree    5a8f54d0e7cc8e4285ada5df40773e92b017718d
parent  656ea1cc8ccfc2f8f5dd8969a472f99e9409403f
...
8
9
10
 
 
11
12
13
14
15
16
 
 
 
17
18
19
...
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
...
8
9
10
11
12
13
14
15
16
17
 
18
19
20
21
22
23
...
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
0
@@ -8,12 +8,16 @@
0
 package net.flying_saucer.util {
0
 
0
   import flash.net.SharedObject
0
+ import flash.utils.getTimer
0
+ import flash.utils.setTimeout
0
 
0
   public class Cache {
0
 
0
     // Properties
0
     // =========================================================================
0
- private static var _cache:SharedObject = SharedObject.getLocal('fsCache', '/')
0
+ private static var _cache:SharedObject = SharedObject.getLocal('fsCache', '/'),
0
+ flush_pending:Boolean = false,
0
+ last_flush:int = 0
0
 
0
 
0
     // Public Methods
0
@@ -22,32 +26,45 @@ package net.flying_saucer.util {
0
       _cache.data[key] = null
0
     }
0
 
0
- public static function flush():void {
0
- _cache.flush()
0
- }
0
-
0
     public static function read(key:String):* {
0
       return _cache.data[key]
0
     }
0
     
0
- public static function write(key:String, val:*, f:Boolean = false):* {
0
+ public static function write(key:String, val:*):* {
0
       _cache.data[key] = val
0
- if(f) flush()
0
+ flush()
0
       return val
0
     }
0
     
0
     // Public Methods for Cached Arrays
0
     // =========================================================================
0
- public static function append(key:String, val:*, f:Boolean = false):Array {
0
+ public static function append(key:String, val:*):Array {
0
       var a:Array = read(key) || []
0
       a.push(val)
0
- return write(key, a, f)
0
+ return write(key, a)
0
     }
0
     
0
- public static function prepend(key:String, val:*, f:Boolean = false):Array {
0
+ public static function prepend(key:String, val:*):Array {
0
       var a:Array = read(key) || []
0
       a.unshift(val)
0
- return write(key, a, f)
0
+ return write(key, a)
0
+ }
0
+
0
+ // Private Methods
0
+ // =========================================================================
0
+
0
+ // Flush with safety mechanism to flush to disk no more than once per second
0
+ //
0
+ private static function flush():void {
0
+ var time_since_last_flush:int = getTimer() - last_flush
0
+ if(time_since_last_flush > 1000) {
0
+ last_flush = getTimer()
0
+ flush_pending = false
0
+ _cache.flush()
0
+ } else if(!flush_pending) {
0
+ flush_pending = true
0
+ setTimeout(flush, 1001 - time_since_last_flush)
0
+ }
0
     }
0
 
0
   }

Comments

    No one has commented yet.