expire_cache 1.0.6

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 63

expire_cache #

build status

A dart package provides FIFO cache and its entries will expire according to time. Also proviodes mutex like method for search usage. Check out example/async_search_example.dart.

If you want to implement SearchDelegate in your app, you will have to cache your search results if you don't want to call your search backend for multiple times on the same query.

See:

https://github.com/flutter/flutter/issues/11655#issuecomment-412413030

https://github.com/flutter/flutter/issues/26759

Because this is related to search, it is valuable to expire the cache after a period of time, to give user fresh search result. And this is the goal for this package, to develop an expire by fix time cache.

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Development #

Run test #

pub run test test/

Examples #

find our test file to see how to use.

Normal Cache Function #

final sizeLimit = 3;
final expireDuration = Duration(seconds: 120);
ExpireCache<int, int> cache = ExpireCache<int, int>(expireDuration: expireDuration, sizeLimit: 3);
for (int i = 0; i < sizeLimit; i++) {
    cache.set(i, i);
print(cache.get(0)); // 0
import 'package:expire_cache/expire_cache.dart';

class _SearchObjectWithMutex {
  static int cacheSetCount = 0;
  static void getInflightOrSet(
      ExpireCache<String, String> cache, String key, String value) async {
    if (!cache.isKeyInFlightOrInCache(key)) {
      cache.markAsInFlight(key);
    } else {
      await cache.get(key);
      return;
    }
    cacheSetCount++;
    await cache.set(key, value);
  }
}

class _SearchObjectWithoutMutex {
  static int cacheSetCount = 0;
  static void getOrSet(
      ExpireCache<String, String> cache, String key, String value) async {
    if (await cache.get(key) != null) {
      return;
    }
    cacheSetCount++;
    await cache.set(key, value);
  }
}

void main() async {
  ExpireCache<String, String> cache = ExpireCache<String, String>();
  _SearchObjectWithMutex.getInflightOrSet(cache, 'key', 'value');
  await _SearchObjectWithMutex.getInflightOrSet(cache, 'key', 'value');
  // Cache should only be set once.
  print(
      'with mutex ${_SearchObjectWithMutex.cacheSetCount}'); // 1, set is called only once.

  cache.clear();
  _SearchObjectWithoutMutex.getOrSet(cache, 'key', 'value2');
  await _SearchObjectWithoutMutex.getOrSet(cache, 'key', 'value2');
  // Cache should only be set once.
  print(
      'without mutex ${_SearchObjectWithoutMutex.cacheSetCount}'); // 2, because the get/set pair are run at the same time, both get will get null.
}

1.0.6 - 2019.01.24 #

  • Update documents.
  • Added clear method.

1.0.5 - 2019.01.24 #

  • Remove blocking get and set methods.
  • Add method to handle the case when get and set are fired at the same time(and cache will be set twice).

1.0.4 - 2019.01.24 #

  • Add blocking get and set methods.

1.0.3 - 2019.01.23 #

  • Update sdk version as dev to be compatible with flutter.

1.0.2 - 2019.01.20 #

1.0.1 - 2019.01.20 #

0.0.1 - 2019.01.19 #

  • A usable simple expire cache.

example/example.dart

import 'package:expire_cache/expire_cache.dart';
import 'package:fake_async/fake_async.dart';

void main() {
  new FakeAsync().run((async) {
    final sizeLimit = 3;
    final expireDuration = Duration(seconds: 120);
    ExpireCache<int, int> cache =
        ExpireCache<int, int>(expireDuration: expireDuration, sizeLimit: 3);
    for (int i = 0; i < sizeLimit; i++) {
      // 0, 1, 2
      cache.set(i, i);
    }
    cache.set(sizeLimit, sizeLimit);
    print(cache.length()); // size is 3
    cache.set(sizeLimit + 1, sizeLimit + 1);
    print(cache.length()); // size is still 3
    async.elapse(Duration(seconds: 160));
    for (int i = 0; i < sizeLimit; i++) {
      // 0, 1, 2
      cache.get(i);
    }
    print(cache.length()); // size is 3
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  expire_cache: ^1.0.6

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:expire_cache/expire_cache.dart';
  
Version Uploaded Documentation Archive
1.0.6 Jan 25, 2019 Go to the documentation of expire_cache 1.0.6 Download expire_cache 1.0.6 archive
1.0.5 Jan 24, 2019 Go to the documentation of expire_cache 1.0.5 Download expire_cache 1.0.5 archive
1.0.4 Jan 24, 2019 Go to the documentation of expire_cache 1.0.4 Download expire_cache 1.0.4 archive
1.0.3 Jan 23, 2019 Go to the documentation of expire_cache 1.0.3 Download expire_cache 1.0.3 archive
1.0.2 Jan 21, 2019 Go to the documentation of expire_cache 1.0.2 Download expire_cache 1.0.2 archive
1.0.1 Jan 21, 2019 Go to the documentation of expire_cache 1.0.1 Download expire_cache 1.0.1 archive
0.0.1 Jan 19, 2019 Go to the documentation of expire_cache 0.0.1 Download expire_cache 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
26
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.61.0 <3.0.0
clock ^1.0.1 1.0.1
Transitive dependencies
meta 1.1.7
Dev dependencies
fake_async ^1.0.1
test ^1.0.0