Please log in to watch this conference skillscast.
Consider what it means to be a cache. You need to be able to
(1) associate a key with a value and
(2) get some value given a key if such a value exists.
That's basically it. Caches tend to appear in layers. In a CPU, memory reads check L1, then L2, then L3, then RAM. When you want to load an image, you first check RAM, then disk, and finally network.
In a mobile app, if you don't nail your cache code, your users will suffer. Excessive networking causes both battery and data-plan drain. You can help ensure a clean correct implementation by combining caches. Given two caches A and B, A
on-top-of B means first check A, fallthrough to B, then write back to A. Now you can define a monoid for caches. Monoids imply easy composition. Easy composition means reasoning about our code becomes easy.
Such an abstraction isn't possible to express both statically and generically in languages like Objective-C and Java, but you can with Swift's strong type system. The caching library Carlos provides the foundation upon which you can build prefetching and other useful transformations. Adopting such a system simplified our codebase: Caches became reusable legos. But the complexity doesn't just all go away, it's just hidden. When an abstraction is so nice, it's tempting to think of the actual caches and cache glue as black-boxes. There was once a reference cycle inside the library that led to large bitmaps leaking and phones running out of memory. It's important to actually think about the implementation details in detail, since these are your building blocks for the rest of the system. When your building blocks are stable, your building is stable.
Caching and prefetching are necessary for mobile apps. In this talk, Brandon will share with you how to think about caches as monoids, how a monoidal caching system can simplify our jobs as software engineers, and what real-world problems you ran into when putting such a system into production.
YOU MAY ALSO LIKE:
- Bringing Swift enums to Objective-C with macros (SkillsCast recorded in March 2017)
- Brian Egan's Flutter and Dart Workshop (in London on 22nd - 23rd October 2019)
- Lightbend Akka for Scala - Professional (in London on 11th - 12th November 2019)
- droidcon London 2019 (in London on 24th - 25th October 2019)
- iOSCon 2020 - The conference for iOS and Swift Developers (in London on 19th - 20th March 2020)
- Clean Architecture using BLoC & Voyager: DI & the Widget Router (in London on 21st October 2019)
- The Five Stages of Data: a holistic approach to data analytics and BI (in London on 21st October 2019)
- Cleaning the Big Picture: Creating Maintainable Mobile Games in Haskell (SkillsCast recorded in October 2019)
- Core Image: Great When It Works (SkillsCast recorded in August 2019)
Composable Caching in Swift
"Brandon Kase brings typed functional programming to weird places. He has shipped production code on Android with Kotlin, iOS with Swift and React Native, and Web with JS/Flow/React. Brandon is an iOS core-experience engineer at Pinterest who came across functional programming while pursuing a B.S. in Computer Science from Carnegie Mellon University. Brandon is excited that strong static typing and functional programming are becoming mainstream, and believes that techniques once reserved for academia will help industry produce more reliable software. He is in general fascinated by anything and everything related to well-designed programming languages and libraries.