Skip to content

Caching

Caching of timeseries data points is supported

Usage

The timeseries.get_data_points accepts a cache boolean which enables storing and extracting data from a cache instead of getting the data from the API. By default, caching is enabled (True).

The example below shows and times

  • Getting data from API
  • Getting data from API and storing it in cache
  • Getting data from cache
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Using caching and timing get_data_point with and without caching
"""

from modeltestsdk import Client
import time
client = Client()


# timeseries id
ts_id = UUID


# Timing get_data_points without caching
t = time.time()
ts_data = client.timeseries.get_data_points(ts_id=ts_id, all_data=True, cache=False)
elapsed = time.time() - t

print(f'Get, no caching: {elapsed} s')

# Timing get_data_points with caching, storing the results to sqlite
t = time.time()
ts_data = client.timeseries.get_data_points(ts_id=ts_id, all_data=True)
elapsed = time.time() - t

print(f'Get, with caching: {elapsed} s')

# Timing get_data_points with caching, extracting the results from sqlite
t = time.time()
ts_data = client.timeseries.get_data_points(ts_id=ts_id, all_data=True)
elapsed = time.time() - t

print(f'Get from cache: {elapsed} s')

This behaviour assumes that the specific time series has not been cached previously

Cache expiration and clearing

The cache is set with an expiry time of 7 days. Data is now explicitly deleted after 7 days, but a new call of get_data_points will replace the existing data.

Note

Caching is related to the API call. Modified data behind the same API call will not be availble if the API call has been cached (and not expired).

Note

It is good practise to clear the cache when new data is posted

The cache can be manually cleared by the client's clear_cache() method

1
2
3
4
5
6
7
8
9
"""
Clearing cache example
"""

from modeltestsdk import Client
client = Client()

# clearing cache
client.clear_cache()

Under the hood

The SDK will establish a SQLite file (mtdb.sqlite) in a local cache folder

  • C:\Users\user\AppData\Local\ for Windows
  • /home/user/.cache/ for Linux,
  • /Users/user/Library/Caches/ for macOS

Note

Deleting this file will remove all cached data, but not affect the modeltestSDK otherwise.