You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by "Erik J. Thomas" <er...@linqto.com> on 2018/10/26 22:35:53 UTC

Persisting ContentCache to disk between sessions

Hey all:

Have any of you come across an example of persisting a ContentCache to disk and reading it back on app launch? We have lots of icons and logos for hundreds of companies and they don't change between sessions. 

Like this (doesn't work though). ImageCache is a subclass of ContentCache to get access to the protected cachedData:Dictionary which is key/value pairs, with key as URL to image, and value is bitmapData.

public function saveImageCache(imageCache:ImageCache):void {
   var cacheFile:File = File.applicationStorageDirectory;
   cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
   if (cacheFile.exists) {
      cacheFile.deleteFile();
   }
   var fileStream:FileStream = new FileStream();
   fileStream.open(cacheFile, FileMode.WRITE);
   fileStream.writeObject(imageCache.getEntries()); // this is a Dictionary with byte array for image as value
   fileStream.close();
}

The writeObject API of FileStream does not marshal the bitmapData to disk. 

/**
 * Loads a persisted image cache from disk.
 *
 * @return ContentCache
 */
public function loadImageCache():ImageCache {
   var cacheFile:File = File.applicationStorageDirectory;
   cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
   if (cacheFile.exists) {
      var fileStream:FileStream = new FileStream();
      fileStream.open(cacheFile, FileMode.READ);
      var entries:Dictionary = fileStream.readObject() as Dictionary;
      fileStream.close();
      var imageCache:ImageCache = new ImageCache();
      imageCache.loadEntries(entries);
      return imageCache;
   }
   return null;
}

The entries variable does populate with all the keys as URLs, but the values are null. FileStream won't read just raw binary data in this way.

I don't want to have to save every image using an encoder into separate files and then load them all back if I can help it. 

Just seems FileStream should be able to just write a blob of binary data to disk and retrieve it "as-is" but it doesn't or I can't find the way.

Thanks for your suggestions.

Erik

Re: Persisting ContentCache to disk between sessions

Posted by Alex Harui <ah...@adobe.com.INVALID>.
Volunteers are welcome to provide a patch that makes ContentCache serializable.  IIRC, lots of Flash classes are not serializable, but IExternalizable allows you to define custom serialization.

-Alex

On 10/29/18, 10:22 AM, "Erik Thomas" <er...@icloud.com> wrote:

    Thanks everyone for your ideas. 
    
    Alex, I already knew how to do the serialization by encoding/decoding, and I may go down that path someday, but if FileStream would just serialize a ContentCache (seems like a common want), it would have been a quick improvement to app responsiveness. 
    
    Having to write my own serializing logic pushes this down on our priorities list since improving app responsiveness is more of a "nice to have" and not a high priority.
    
    I'm going to drop this idea for now.
    
    Thanks again for your insights, everyone.
    
    Erik
    
    


Re: Persisting ContentCache to disk between sessions

Posted by Erik Thomas <er...@icloud.com>.
Hi Javier:

With your suggestion, every new company onboarded will download the entire sprite. Sure it's only one image but it's 1500+ x 200px square. Our current architecture just downloads the single image that's new or changed at about 500 bytes.

Using a sprite is definitely an interesting option for down the road, perhaps only downloaded on launch and then update individual images on updates, but I have to get back to higher priority work. 

Thanks for sharing.

Erik

On Oct 29, 2018, at 5:07 PM, Javier Guerrero García <ja...@gmail.com> wrote:

You're welcome, but... what's the exact problem with spriting in that
scenario? You could generate the sprite bitmap dynamically at any given
time (just paste every known logo one below another), even with bare PHP
(or needless to say with ImageMagic libraries), and then just poll for the
last modified date of the generated sprite bitmap :-?

On Mon, Oct 29, 2018 at 10:48 PM Erik Thomas <er...@icloud.com> wrote:

> Hi Javier:
> 
> The thing is we have 52 investor groups in 26 countries on 3 continents
> and they each have admins entering information that is then displayed in
> our app. Part of that information are companies that are presenting at
> regular investor meetings in these different countries. These companies all
> have logos. Our app polls for changes every 20 seconds, and if someone has
> added a new company, all users worldwide will see it appear in the app
> within 20 seconds.
> 
> But thanks for your suggestion!
> 
> Erik
> 
> On Oct 29, 2018, at 1:30 PM, Javier Guerrero García <ja...@gmail.com>
> wrote:
> 
> Just my 2 cents: have you considered getting rid of everything and just use
> one sprite image for all the logos?
> 
> https://www.w3schools.com/css/css_image_sprites.asp
> 
> You minimize connections to just 1 download, that if properly compressed
> and if logos share a fair amount to colors, could get the size per logo to
> a ridiculously few bytes. All item renderers would use the same image (100%
> bitmapCache hits), but at the cost of downloading the whole bitmap each
> time one of the logos change (how often?).
> 
> It could be a fairly simple solution, and may it can improve your app
> responsiveness without too much trouble.
> 
> Hope it helps ;-)
> 
> On Mon, Oct 29, 2018 at 6:22 PM Erik Thomas <er...@icloud.com>
> wrote:
> 
>> Thanks everyone for your ideas.
>> 
>> Alex, I already knew how to do the serialization by encoding/decoding,
> and
>> I may go down that path someday, but if FileStream would just serialize a
>> ContentCache (seems like a common want), it would have been a quick
>> improvement to app responsiveness.
>> 
>> Having to write my own serializing logic pushes this down on our
>> priorities list since improving app responsiveness is more of a "nice to
>> have" and not a high priority.
>> 
>> I'm going to drop this idea for now.
>> 
>> Thanks again for your insights, everyone.
>> 
>> Erik
>> 
>> 
> 
> 


Re: Persisting ContentCache to disk between sessions

Posted by Javier Guerrero García <ja...@gmail.com>.
You're welcome, but... what's the exact problem with spriting in that
scenario? You could generate the sprite bitmap dynamically at any given
time (just paste every known logo one below another), even with bare PHP
(or needless to say with ImageMagic libraries), and then just poll for the
last modified date of the generated sprite bitmap :-?

On Mon, Oct 29, 2018 at 10:48 PM Erik Thomas <er...@icloud.com> wrote:

> Hi Javier:
>
> The thing is we have 52 investor groups in 26 countries on 3 continents
> and they each have admins entering information that is then displayed in
> our app. Part of that information are companies that are presenting at
> regular investor meetings in these different countries. These companies all
> have logos. Our app polls for changes every 20 seconds, and if someone has
> added a new company, all users worldwide will see it appear in the app
> within 20 seconds.
>
> But thanks for your suggestion!
>
> Erik
>
> On Oct 29, 2018, at 1:30 PM, Javier Guerrero García <ja...@gmail.com>
> wrote:
>
> Just my 2 cents: have you considered getting rid of everything and just use
> one sprite image for all the logos?
>
> https://www.w3schools.com/css/css_image_sprites.asp
>
> You minimize connections to just 1 download, that if properly compressed
> and if logos share a fair amount to colors, could get the size per logo to
> a ridiculously few bytes. All item renderers would use the same image (100%
> bitmapCache hits), but at the cost of downloading the whole bitmap each
> time one of the logos change (how often?).
>
> It could be a fairly simple solution, and may it can improve your app
> responsiveness without too much trouble.
>
> Hope it helps ;-)
>
> On Mon, Oct 29, 2018 at 6:22 PM Erik Thomas <er...@icloud.com>
> wrote:
>
> > Thanks everyone for your ideas.
> >
> > Alex, I already knew how to do the serialization by encoding/decoding,
> and
> > I may go down that path someday, but if FileStream would just serialize a
> > ContentCache (seems like a common want), it would have been a quick
> > improvement to app responsiveness.
> >
> > Having to write my own serializing logic pushes this down on our
> > priorities list since improving app responsiveness is more of a "nice to
> > have" and not a high priority.
> >
> > I'm going to drop this idea for now.
> >
> > Thanks again for your insights, everyone.
> >
> > Erik
> >
> >
>
>

Re: Persisting ContentCache to disk between sessions

Posted by Erik Thomas <er...@icloud.com>.
Hi Javier:

The thing is we have 52 investor groups in 26 countries on 3 continents and they each have admins entering information that is then displayed in our app. Part of that information are companies that are presenting at regular investor meetings in these different countries. These companies all have logos. Our app polls for changes every 20 seconds, and if someone has added a new company, all users worldwide will see it appear in the app within 20 seconds.

But thanks for your suggestion!

Erik

On Oct 29, 2018, at 1:30 PM, Javier Guerrero García <ja...@gmail.com> wrote:

Just my 2 cents: have you considered getting rid of everything and just use
one sprite image for all the logos?

https://www.w3schools.com/css/css_image_sprites.asp

You minimize connections to just 1 download, that if properly compressed
and if logos share a fair amount to colors, could get the size per logo to
a ridiculously few bytes. All item renderers would use the same image (100%
bitmapCache hits), but at the cost of downloading the whole bitmap each
time one of the logos change (how often?).

It could be a fairly simple solution, and may it can improve your app
responsiveness without too much trouble.

Hope it helps ;-)

On Mon, Oct 29, 2018 at 6:22 PM Erik Thomas <er...@icloud.com> wrote:

> Thanks everyone for your ideas.
> 
> Alex, I already knew how to do the serialization by encoding/decoding, and
> I may go down that path someday, but if FileStream would just serialize a
> ContentCache (seems like a common want), it would have been a quick
> improvement to app responsiveness.
> 
> Having to write my own serializing logic pushes this down on our
> priorities list since improving app responsiveness is more of a "nice to
> have" and not a high priority.
> 
> I'm going to drop this idea for now.
> 
> Thanks again for your insights, everyone.
> 
> Erik
> 
> 


Re: Persisting ContentCache to disk between sessions

Posted by Javier Guerrero García <ja...@gmail.com>.
Just my 2 cents: have you considered getting rid of everything and just use
one sprite image for all the logos?

https://www.w3schools.com/css/css_image_sprites.asp

You minimize connections to just 1 download, that if properly compressed
and if logos share a fair amount to colors, could get the size per logo to
a ridiculously few bytes. All item renderers would use the same image (100%
bitmapCache hits), but at the cost of downloading the whole bitmap each
time one of the logos change (how often?).

It could be a fairly simple solution, and may it can improve your app
responsiveness without too much trouble.

Hope it helps ;-)

On Mon, Oct 29, 2018 at 6:22 PM Erik Thomas <er...@icloud.com> wrote:

> Thanks everyone for your ideas.
>
> Alex, I already knew how to do the serialization by encoding/decoding, and
> I may go down that path someday, but if FileStream would just serialize a
> ContentCache (seems like a common want), it would have been a quick
> improvement to app responsiveness.
>
> Having to write my own serializing logic pushes this down on our
> priorities list since improving app responsiveness is more of a "nice to
> have" and not a high priority.
>
> I'm going to drop this idea for now.
>
> Thanks again for your insights, everyone.
>
> Erik
>
>

Re: Persisting ContentCache to disk between sessions

Posted by Erik Thomas <er...@icloud.com>.
Thanks everyone for your ideas. 

Alex, I already knew how to do the serialization by encoding/decoding, and I may go down that path someday, but if FileStream would just serialize a ContentCache (seems like a common want), it would have been a quick improvement to app responsiveness. 

Having to write my own serializing logic pushes this down on our priorities list since improving app responsiveness is more of a "nice to have" and not a high priority.

I'm going to drop this idea for now.

Thanks again for your insights, everyone.

Erik


Re: Persisting ContentCache to disk between sessions

Posted by Alex Harui <ah...@adobe.com.INVALID>.
Hi Erik,

I don't think you can have Flash serialize a Dictionary by passing a Dictionary to FileStream.writeObject.  However, you can certainly iterate the Dictionary and save everything.  And later, read it all back and repopulate the Dictionary.

Personally, when writing custom serialization, I like storing the length of a thing before the actual thing in the data stream.  Makes it easier to deserialize.  With BitmapData.encode you should be able to get a ByteArray to add to the FileStream.

Feel free to ask more questions if you're not sure how to do it.

-Alex

On 10/27/18, 10:13 AM, "Erik Thomas" <er...@icloud.com> wrote:

    Hi Alex:
    
    The icons and logos are loaded from a server on startup, and then kept up to date via a 20 second polling pattern where the app checks if anything has changed on the server. We timestamp all images and when one does change we get latest and update the image in the app.
    
    The way a ContentCache is used for BitmapImage and Image is to set the loader like this:
    
    public static var imageCache:ImageCache = new ImageCache(); // subclass of ContentCache
    _image.contentLoader = Model.imageCache;
    _image.source = "https/someImageUrl"
    
    When the image is first retrieved from the URL, the ContentCache loader will save the image into a Dictionary (key/value pairs) where the key is the URL and the value is the bitmapData of the image.
    
    Then when using ItemRenderers, and they get recycled, the when the image URL changes, ContentCache will look up the URL in it's Dictionary and if it's already been downloaded it will simply display it without having to make an HTTP request. 
    
    ContentCache is a very cool thing the Adobe engineers designed into Flex/Flash and it makes lists with icons and pictures far more responsive.
    
    What I want to do is persist the cache to disk and load it when the user logs in again. If any image have changed since the last time they launched the app, the first 20 second poll will indicate a timestamp has changed and the app will retrieve images with a timestamp after the one returned when polling. So they will always be up to date.
    
    I said the images don't change but that's not actually true, they just don't change often, maybe once a week we'll onboard new organizations and events with images. 
    
    Embedding images is not an option because every new onboarded entity will require a new app build, full regression testing, submission to Apple for review and the entire release process. That's a bad business model.
    
    Anyway, there must be a way in FileStream to save a Dictionary with key as String and value as ByteArray, but I haven't figured it out yet. Was hoping someone else knows how.
    
    Thanks, Alex.
    
    Erik
    
    PS: I know how I can do this, but it's too much work the way I know how, I would have to take each cached image, use pngencoder to encode the bitmapData (ByteArray) to a PNG and save each image as a separate file. Last count our image cache has about 1500 images so that means 1500 files, plus a hash file to match the image URL to a generated filename for the image. Loading will then require reversing the process. If you've used image encoders in Flash/AIR you know they're  pretty slow. If we don't gain any speed, it's not worth the effort.
    
    
    On Oct 26, 2018, at 3:44 PM, Alex Harui <ah...@adobe.com.INVALID> wrote:
    
    Erik,
    
    How do the icons and logos get into the cache in the first place?  If they don't change, is there any reason not to embed them in the application?
    
    -Alex
    
    On 10/26/18, 3:36 PM, "Erik J. Thomas" <er...@linqto.com> wrote:
    
       Hey all:
    
       Have any of you come across an example of persisting a ContentCache to disk and reading it back on app launch? We have lots of icons and logos for hundreds of companies and they don't change between sessions. 
    
       Like this (doesn't work though). ImageCache is a subclass of ContentCache to get access to the protected cachedData:Dictionary which is key/value pairs, with key as URL to image, and value is bitmapData.
    
       public function saveImageCache(imageCache:ImageCache):void {
          var cacheFile:File = File.applicationStorageDirectory;
          cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
          if (cacheFile.exists) {
             cacheFile.deleteFile();
          }
          var fileStream:FileStream = new FileStream();
          fileStream.open(cacheFile, FileMode.WRITE);
          fileStream.writeObject(imageCache.getEntries()); // this is a Dictionary with byte array for image as value
          fileStream.close();
       }
    
       The writeObject API of FileStream does not marshal the bitmapData to disk. 
    
       /**
        * Loads a persisted image cache from disk.
        *
        * @return ContentCache
        */
       public function loadImageCache():ImageCache {
          var cacheFile:File = File.applicationStorageDirectory;
          cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
          if (cacheFile.exists) {
             var fileStream:FileStream = new FileStream();
             fileStream.open(cacheFile, FileMode.READ);
             var entries:Dictionary = fileStream.readObject() as Dictionary;
             fileStream.close();
             var imageCache:ImageCache = new ImageCache();
             imageCache.loadEntries(entries);
             return imageCache;
          }
          return null;
       }
    
       The entries variable does populate with all the keys as URLs, but the values are null. FileStream won't read just raw binary data in this way.
    
       I don't want to have to save every image using an encoder into separate files and then load them all back if I can help it. 
    
       Just seems FileStream should be able to just write a blob of binary data to disk and retrieve it "as-is" but it doesn't or I can't find the way.
    
       Thanks for your suggestions.
    
       Erik
    
    
    


Re: Persisting ContentCache to disk between sessions

Posted by Greg Dove <gr...@gmail.com>.
Oops, I pressed send to early, more info this time :)


I think BitmapData may not be directly serializable to amf. It is in
display package, and although not directly a display object, it might be
subject to same rules.
Here's the type of thing someone did in the past to avoid that:

https://cambiatablog.wordpress.com/2009/09/12/as3-bitmapdata-amf-solution-using-iexternalizable/


Also it would be more compact I think to store the original loaded bytes.

Png and jpeg encoding was always slow with the original action script libs,
but bitmapdata has much faster native encoding methods now. Still maybe not
practical for large volumes of images as a batch, but I have found it to be
much more performance for encoding.

I wonder why you need to reencode when the original cache should have
access to the original loaded bytes (png or jpeg) at some level. It should
be more compact in the final cached file.



On Sun, Oct 28, 2018 at 11:03 AM Greg Dove <gr...@gmail.com> wrote:

> Not an answer to the main question, but a couple of thoughts...
>
> I think BitmapData may not be directly serializable to amf. It is in
> display package, and although not directly a display object, it might be
> subject to same rules.
> Here's the type of thing someone did in the past to avoid that:
> Also it would be more compact I think to store the original loaded bytes.
>
> Png and jpeg encoding was always slow with the original action script
> libs, but bitmapdata has much faster native encoding methods now. Still
> maybe not practical for large volumes of images as a batch, but I have
> found it to be much more performance for encoding.
>
> I wonder why you need to reencode when the original cache should have
> access
>
>
>
> On Sun, 28 Oct 2018, 06:13 Erik Thomas, <er...@icloud.com> wrote:
>
>> Hi Alex:
>>
>> The icons and logos are loaded from a server on startup, and then kept up
>> to date via a 20 second polling pattern where the app checks if anything
>> has changed on the server. We timestamp all images and when one does change
>> we get latest and update the image in the app.
>>
>> The way a ContentCache is used for BitmapImage and Image is to set the
>> loader like this:
>>
>> public static var imageCache:ImageCache = new ImageCache(); // subclass
>> of ContentCache
>> _image.contentLoader = Model.imageCache;
>> _image.source = "https/someImageUrl"
>>
>> When the image is first retrieved from the URL, the ContentCache loader
>> will save the image into a Dictionary (key/value pairs) where the key is
>> the URL and the value is the bitmapData of the image.
>>
>> Then when using ItemRenderers, and they get recycled, the when the image
>> URL changes, ContentCache will look up the URL in it's Dictionary and if
>> it's already been downloaded it will simply display it without having to
>> make an HTTP request.
>>
>> ContentCache is a very cool thing the Adobe engineers designed into
>> Flex/Flash and it makes lists with icons and pictures far more responsive.
>>
>> What I want to do is persist the cache to disk and load it when the user
>> logs in again. If any image have changed since the last time they launched
>> the app, the first 20 second poll will indicate a timestamp has changed and
>> the app will retrieve images with a timestamp after the one returned when
>> polling. So they will always be up to date.
>>
>> I said the images don't change but that's not actually true, they just
>> don't change often, maybe once a week we'll onboard new organizations and
>> events with images.
>>
>> Embedding images is not an option because every new onboarded entity will
>> require a new app build, full regression testing, submission to Apple for
>> review and the entire release process. That's a bad business model.
>>
>> Anyway, there must be a way in FileStream to save a Dictionary with key
>> as String and value as ByteArray, but I haven't figured it out yet. Was
>> hoping someone else knows how.
>>
>> Thanks, Alex.
>>
>> Erik
>>
>> PS: I know how I can do this, but it's too much work the way I know how,
>> I would have to take each cached image, use pngencoder to encode the
>> bitmapData (ByteArray) to a PNG and save each image as a separate file.
>> Last count our image cache has about 1500 images so that means 1500 files,
>> plus a hash file to match the image URL to a generated filename for the
>> image. Loading will then require reversing the process. If you've used
>> image encoders in Flash/AIR you know they're  pretty slow. If we don't gain
>> any speed, it's not worth the effort.
>>
>>
>> On Oct 26, 2018, at 3:44 PM, Alex Harui <ah...@adobe.com.INVALID> wrote:
>>
>> Erik,
>>
>> How do the icons and logos get into the cache in the first place?  If
>> they don't change, is there any reason not to embed them in the application?
>>
>> -Alex
>>
>> On 10/26/18, 3:36 PM, "Erik J. Thomas" <er...@linqto.com> wrote:
>>
>>    Hey all:
>>
>>    Have any of you come across an example of persisting a ContentCache to
>> disk and reading it back on app launch? We have lots of icons and logos for
>> hundreds of companies and they don't change between sessions.
>>
>>    Like this (doesn't work though). ImageCache is a subclass of
>> ContentCache to get access to the protected cachedData:Dictionary which is
>> key/value pairs, with key as URL to image, and value is bitmapData.
>>
>>    public function saveImageCache(imageCache:ImageCache):void {
>>       var cacheFile:File = File.applicationStorageDirectory;
>>       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
>>       if (cacheFile.exists) {
>>          cacheFile.deleteFile();
>>       }
>>       var fileStream:FileStream = new FileStream();
>>       fileStream.open(cacheFile, FileMode.WRITE);
>>       fileStream.writeObject(imageCache.getEntries()); // this is a
>> Dictionary with byte array for image as value
>>       fileStream.close();
>>    }
>>
>>    The writeObject API of FileStream does not marshal the bitmapData to
>> disk.
>>
>>    /**
>>     * Loads a persisted image cache from disk.
>>     *
>>     * @return ContentCache
>>     */
>>    public function loadImageCache():ImageCache {
>>       var cacheFile:File = File.applicationStorageDirectory;
>>       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
>>       if (cacheFile.exists) {
>>          var fileStream:FileStream = new FileStream();
>>          fileStream.open(cacheFile, FileMode.READ);
>>          var entries:Dictionary = fileStream.readObject() as Dictionary;
>>          fileStream.close();
>>          var imageCache:ImageCache = new ImageCache();
>>          imageCache.loadEntries(entries);
>>          return imageCache;
>>       }
>>       return null;
>>    }
>>
>>    The entries variable does populate with all the keys as URLs, but the
>> values are null. FileStream won't read just raw binary data in this way.
>>
>>    I don't want to have to save every image using an encoder into
>> separate files and then load them all back if I can help it.
>>
>>    Just seems FileStream should be able to just write a blob of binary
>> data to disk and retrieve it "as-is" but it doesn't or I can't find the way.
>>
>>    Thanks for your suggestions.
>>
>>    Erik
>>
>>
>>

Re: Persisting ContentCache to disk between sessions

Posted by Greg Dove <gr...@gmail.com>.
Not an answer to the main question, but a couple of thoughts...

I think BitmapData may not be directly serializable to amf. It is in
display package, and although not directly a display object, it might be
subject to same rules.
Here's the type of thing someone did in the past to avoid that:
Also it would be more compact I think to store the original loaded bytes.

Png and jpeg encoding was always slow with the original action script libs,
but bitmapdata has much faster native encoding methods now. Still maybe not
practical for large volumes of images as a batch, but I have found it to be
much more performance for encoding.

I wonder why you need to reencode when the original cache should have access



On Sun, 28 Oct 2018, 06:13 Erik Thomas, <er...@icloud.com> wrote:

> Hi Alex:
>
> The icons and logos are loaded from a server on startup, and then kept up
> to date via a 20 second polling pattern where the app checks if anything
> has changed on the server. We timestamp all images and when one does change
> we get latest and update the image in the app.
>
> The way a ContentCache is used for BitmapImage and Image is to set the
> loader like this:
>
> public static var imageCache:ImageCache = new ImageCache(); // subclass of
> ContentCache
> _image.contentLoader = Model.imageCache;
> _image.source = "https/someImageUrl"
>
> When the image is first retrieved from the URL, the ContentCache loader
> will save the image into a Dictionary (key/value pairs) where the key is
> the URL and the value is the bitmapData of the image.
>
> Then when using ItemRenderers, and they get recycled, the when the image
> URL changes, ContentCache will look up the URL in it's Dictionary and if
> it's already been downloaded it will simply display it without having to
> make an HTTP request.
>
> ContentCache is a very cool thing the Adobe engineers designed into
> Flex/Flash and it makes lists with icons and pictures far more responsive.
>
> What I want to do is persist the cache to disk and load it when the user
> logs in again. If any image have changed since the last time they launched
> the app, the first 20 second poll will indicate a timestamp has changed and
> the app will retrieve images with a timestamp after the one returned when
> polling. So they will always be up to date.
>
> I said the images don't change but that's not actually true, they just
> don't change often, maybe once a week we'll onboard new organizations and
> events with images.
>
> Embedding images is not an option because every new onboarded entity will
> require a new app build, full regression testing, submission to Apple for
> review and the entire release process. That's a bad business model.
>
> Anyway, there must be a way in FileStream to save a Dictionary with key as
> String and value as ByteArray, but I haven't figured it out yet. Was hoping
> someone else knows how.
>
> Thanks, Alex.
>
> Erik
>
> PS: I know how I can do this, but it's too much work the way I know how, I
> would have to take each cached image, use pngencoder to encode the
> bitmapData (ByteArray) to a PNG and save each image as a separate file.
> Last count our image cache has about 1500 images so that means 1500 files,
> plus a hash file to match the image URL to a generated filename for the
> image. Loading will then require reversing the process. If you've used
> image encoders in Flash/AIR you know they're  pretty slow. If we don't gain
> any speed, it's not worth the effort.
>
>
> On Oct 26, 2018, at 3:44 PM, Alex Harui <ah...@adobe.com.INVALID> wrote:
>
> Erik,
>
> How do the icons and logos get into the cache in the first place?  If they
> don't change, is there any reason not to embed them in the application?
>
> -Alex
>
> On 10/26/18, 3:36 PM, "Erik J. Thomas" <er...@linqto.com> wrote:
>
>    Hey all:
>
>    Have any of you come across an example of persisting a ContentCache to
> disk and reading it back on app launch? We have lots of icons and logos for
> hundreds of companies and they don't change between sessions.
>
>    Like this (doesn't work though). ImageCache is a subclass of
> ContentCache to get access to the protected cachedData:Dictionary which is
> key/value pairs, with key as URL to image, and value is bitmapData.
>
>    public function saveImageCache(imageCache:ImageCache):void {
>       var cacheFile:File = File.applicationStorageDirectory;
>       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
>       if (cacheFile.exists) {
>          cacheFile.deleteFile();
>       }
>       var fileStream:FileStream = new FileStream();
>       fileStream.open(cacheFile, FileMode.WRITE);
>       fileStream.writeObject(imageCache.getEntries()); // this is a
> Dictionary with byte array for image as value
>       fileStream.close();
>    }
>
>    The writeObject API of FileStream does not marshal the bitmapData to
> disk.
>
>    /**
>     * Loads a persisted image cache from disk.
>     *
>     * @return ContentCache
>     */
>    public function loadImageCache():ImageCache {
>       var cacheFile:File = File.applicationStorageDirectory;
>       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
>       if (cacheFile.exists) {
>          var fileStream:FileStream = new FileStream();
>          fileStream.open(cacheFile, FileMode.READ);
>          var entries:Dictionary = fileStream.readObject() as Dictionary;
>          fileStream.close();
>          var imageCache:ImageCache = new ImageCache();
>          imageCache.loadEntries(entries);
>          return imageCache;
>       }
>       return null;
>    }
>
>    The entries variable does populate with all the keys as URLs, but the
> values are null. FileStream won't read just raw binary data in this way.
>
>    I don't want to have to save every image using an encoder into separate
> files and then load them all back if I can help it.
>
>    Just seems FileStream should be able to just write a blob of binary
> data to disk and retrieve it "as-is" but it doesn't or I can't find the way.
>
>    Thanks for your suggestions.
>
>    Erik
>
>
>

Re: Persisting ContentCache to disk between sessions

Posted by Erik Thomas <er...@icloud.com>.
Hi Alex:

The icons and logos are loaded from a server on startup, and then kept up to date via a 20 second polling pattern where the app checks if anything has changed on the server. We timestamp all images and when one does change we get latest and update the image in the app.

The way a ContentCache is used for BitmapImage and Image is to set the loader like this:

public static var imageCache:ImageCache = new ImageCache(); // subclass of ContentCache
_image.contentLoader = Model.imageCache;
_image.source = "https/someImageUrl"

When the image is first retrieved from the URL, the ContentCache loader will save the image into a Dictionary (key/value pairs) where the key is the URL and the value is the bitmapData of the image.

Then when using ItemRenderers, and they get recycled, the when the image URL changes, ContentCache will look up the URL in it's Dictionary and if it's already been downloaded it will simply display it without having to make an HTTP request. 

ContentCache is a very cool thing the Adobe engineers designed into Flex/Flash and it makes lists with icons and pictures far more responsive.

What I want to do is persist the cache to disk and load it when the user logs in again. If any image have changed since the last time they launched the app, the first 20 second poll will indicate a timestamp has changed and the app will retrieve images with a timestamp after the one returned when polling. So they will always be up to date.

I said the images don't change but that's not actually true, they just don't change often, maybe once a week we'll onboard new organizations and events with images. 

Embedding images is not an option because every new onboarded entity will require a new app build, full regression testing, submission to Apple for review and the entire release process. That's a bad business model.

Anyway, there must be a way in FileStream to save a Dictionary with key as String and value as ByteArray, but I haven't figured it out yet. Was hoping someone else knows how.

Thanks, Alex.

Erik

PS: I know how I can do this, but it's too much work the way I know how, I would have to take each cached image, use pngencoder to encode the bitmapData (ByteArray) to a PNG and save each image as a separate file. Last count our image cache has about 1500 images so that means 1500 files, plus a hash file to match the image URL to a generated filename for the image. Loading will then require reversing the process. If you've used image encoders in Flash/AIR you know they're  pretty slow. If we don't gain any speed, it's not worth the effort.


On Oct 26, 2018, at 3:44 PM, Alex Harui <ah...@adobe.com.INVALID> wrote:

Erik,

How do the icons and logos get into the cache in the first place?  If they don't change, is there any reason not to embed them in the application?

-Alex

On 10/26/18, 3:36 PM, "Erik J. Thomas" <er...@linqto.com> wrote:

   Hey all:

   Have any of you come across an example of persisting a ContentCache to disk and reading it back on app launch? We have lots of icons and logos for hundreds of companies and they don't change between sessions. 

   Like this (doesn't work though). ImageCache is a subclass of ContentCache to get access to the protected cachedData:Dictionary which is key/value pairs, with key as URL to image, and value is bitmapData.

   public function saveImageCache(imageCache:ImageCache):void {
      var cacheFile:File = File.applicationStorageDirectory;
      cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
      if (cacheFile.exists) {
         cacheFile.deleteFile();
      }
      var fileStream:FileStream = new FileStream();
      fileStream.open(cacheFile, FileMode.WRITE);
      fileStream.writeObject(imageCache.getEntries()); // this is a Dictionary with byte array for image as value
      fileStream.close();
   }

   The writeObject API of FileStream does not marshal the bitmapData to disk. 

   /**
    * Loads a persisted image cache from disk.
    *
    * @return ContentCache
    */
   public function loadImageCache():ImageCache {
      var cacheFile:File = File.applicationStorageDirectory;
      cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
      if (cacheFile.exists) {
         var fileStream:FileStream = new FileStream();
         fileStream.open(cacheFile, FileMode.READ);
         var entries:Dictionary = fileStream.readObject() as Dictionary;
         fileStream.close();
         var imageCache:ImageCache = new ImageCache();
         imageCache.loadEntries(entries);
         return imageCache;
      }
      return null;
   }

   The entries variable does populate with all the keys as URLs, but the values are null. FileStream won't read just raw binary data in this way.

   I don't want to have to save every image using an encoder into separate files and then load them all back if I can help it. 

   Just seems FileStream should be able to just write a blob of binary data to disk and retrieve it "as-is" but it doesn't or I can't find the way.

   Thanks for your suggestions.

   Erik



Re: Persisting ContentCache to disk between sessions

Posted by Alex Harui <ah...@adobe.com.INVALID>.
Erik,

How do the icons and logos get into the cache in the first place?  If they don't change, is there any reason not to embed them in the application?

-Alex

On 10/26/18, 3:36 PM, "Erik J. Thomas" <er...@linqto.com> wrote:

    Hey all:
    
    Have any of you come across an example of persisting a ContentCache to disk and reading it back on app launch? We have lots of icons and logos for hundreds of companies and they don't change between sessions. 
    
    Like this (doesn't work though). ImageCache is a subclass of ContentCache to get access to the protected cachedData:Dictionary which is key/value pairs, with key as URL to image, and value is bitmapData.
    
    public function saveImageCache(imageCache:ImageCache):void {
       var cacheFile:File = File.applicationStorageDirectory;
       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
       if (cacheFile.exists) {
          cacheFile.deleteFile();
       }
       var fileStream:FileStream = new FileStream();
       fileStream.open(cacheFile, FileMode.WRITE);
       fileStream.writeObject(imageCache.getEntries()); // this is a Dictionary with byte array for image as value
       fileStream.close();
    }
    
    The writeObject API of FileStream does not marshal the bitmapData to disk. 
    
    /**
     * Loads a persisted image cache from disk.
     *
     * @return ContentCache
     */
    public function loadImageCache():ImageCache {
       var cacheFile:File = File.applicationStorageDirectory;
       cacheFile = cacheFile.resolvePath(IMAGE_CACHE_FILE_NAME);
       if (cacheFile.exists) {
          var fileStream:FileStream = new FileStream();
          fileStream.open(cacheFile, FileMode.READ);
          var entries:Dictionary = fileStream.readObject() as Dictionary;
          fileStream.close();
          var imageCache:ImageCache = new ImageCache();
          imageCache.loadEntries(entries);
          return imageCache;
       }
       return null;
    }
    
    The entries variable does populate with all the keys as URLs, but the values are null. FileStream won't read just raw binary data in this way.
    
    I don't want to have to save every image using an encoder into separate files and then load them all back if I can help it. 
    
    Just seems FileStream should be able to just write a blob of binary data to disk and retrieve it "as-is" but it doesn't or I can't find the way.
    
    Thanks for your suggestions.
    
    Erik