Batman Batman - 1 year ago 40
AngularJS Question

Don't understand the need for $cacheFactory if Factories are singletons

I'm relatively new to AngularJs and I'm trying to build a single page application with it. My application runs within a content management site. I have 3 factories for user, group and site functions.

I'm interested in using angular's $cacheFactory but I'm not sure what the benefit is if my data remains in the factory since it's a singleton.

For example, my user factory has an object called allUsers. This object is used through my application for different views. Intuitively, it seems like it's something that should be cached but I'm having a hard time understanding why since this object is in the user factory which can be access from any of my controllers.

Answer Source

CacheFactory is a factory:

The purpose of the Factory Method pattern is to define an interface for creating an object but deferring instantiation of the object to subclasses.

which creates new instances each time an object of its type is created, unlike a Singleton:

The intent of the Singleton pattern is to ensure that only one instance of a class is loaded, and that the instance provides a global point of access.

In Angular, the CacheFactory offers the following benefits over other solutions:

  • Caches can be injected into a filter, unlike controller scopes
  • Caches can be enumerated
  • Caches can be easily destroyed or redefined
  • Caches are not tied to digest data binding, unlike controller scopes
  • Caches are not tied to browser permissions, unlike cookies or localStorage/sessionStorage

I'm not sure what the benefit is if my data remains in the factory

CacheFactory offers the following features:

  • 'should create cache with defined capacity'
  • 'should blow away the entire cache via removeAll and start evicting when full'
  • 'should correctly refresh and evict items if operations are chained'

But CacheFactory also has the following unexpected behaviors:

  • 'should purge the next entry if the stalest one was removed'
  • 'should only decrement size when an element is actually removed via remove'