Skip to content

Commit

Permalink
Add TTL support for cache and update README with caching details.
Browse files Browse the repository at this point in the history
  • Loading branch information
deeravenger committed Aug 22, 2024
1 parent eba9a66 commit 3db5fea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ If you want to load LilTag asynchronously and initialize it only after the scrip

## Usage

### 1. Loading Configuration from a URL
### Loading Configuration

#### 1. Loading Configuration from a URL
You can initialize LilTag by providing a URL to a JSON configuration file. The configuration file should contain the tags you want to inject into your web page.

```javascript
const lilTag = new LilTag("path_or_url/to/liltag_config.json");
lilTag.init();
```

#### JSON Configuration Example
##### JSON Configuration Example
```json
{
"tags": [
Expand Down Expand Up @@ -91,7 +93,7 @@ lilTag.init();
}
```

### 2. Providing the Configuration Directly
#### 2. Providing the Configuration Directly
If you prefer to provide the configuration directly within your code, you can pass it as an object to the lilTagInit function.

```javascript
Expand All @@ -117,6 +119,17 @@ const lilTag = new LilTag({
lilTag.init();
```

### Enabling Caching

You can enable caching to avoid fetching the configuration on every page load. The enableCache() method allows you to specify a TTL (time-to-live) in seconds.

```javascript
// Enable caching with a TTL of 2 hours (7200 seconds)
const lilTag = new LilTag('https://example.com/liltag-config.json');
lilTag.enableCache(7200);
lilTag.init();
```

## Configuration Options
Each tag in the configuration file or object should have the following properties:

Expand Down
15 changes: 12 additions & 3 deletions dist/liltag.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ class LilTag {
constructor(config) {
this.config = config;
this.cacheEnabled = false;
this.cacheTTL = LilTag.CACHE_DEFAULT_TTL;
}
enableCache() {
/**
* Enable caching with a specific TTL (time-to-live) in seconds.
* @param ttl - Time in seconds for which the cache is valid.
*/
enableCache(ttl = LilTag.CACHE_DEFAULT_TTL) {
this.cacheEnabled = true;
this.cacheTTL = ttl;
}
init() {
if (this.config === "") {
Expand Down Expand Up @@ -91,8 +97,10 @@ class LilTag {
const cachedEntry = cacheData[url];
if (!cachedEntry)
return null;
const oneDay = 24 * 60 * 60 * 1000;
if (Date.now() - cachedEntry.timestamp > oneDay) {
// Calculate TTL in milliseconds
const ttlInMilliseconds = this.cacheTTL * 1000;
// Check if the cache has expired
if (Date.now() - cachedEntry.timestamp > ttlInMilliseconds) {
delete cacheData[url];
localStorage.setItem(LilTag.CACHE_KEY, JSON.stringify(cacheData));
return null;
Expand Down Expand Up @@ -206,6 +214,7 @@ class LilTag {
}
LilTag.DATA_ATTRIBUTE = "data-tag-id";
LilTag.CACHE_KEY = "LilTagConfigCache";
LilTag.CACHE_DEFAULT_TTL = 3600;
exports["default"] = LilTag;

})();
Expand Down
2 changes: 1 addition & 1 deletion dist/liltag.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/liltag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ interface Config {
export default class LilTag {
private static readonly DATA_ATTRIBUTE = "data-tag-id";
private static readonly CACHE_KEY = "LilTagConfigCache";
private static readonly CACHE_DEFAULT_TTL = 3600;
private cacheEnabled: boolean = false;
private cacheTTL: number = LilTag.CACHE_DEFAULT_TTL;

constructor(private config: Config | string) {}

public enableCache(): void {
/**
* Enable caching with a specific TTL (time-to-live) in seconds.
* @param ttl - Time in seconds for which the cache is valid.
*/
public enableCache(ttl: number = LilTag.CACHE_DEFAULT_TTL): void {
this.cacheEnabled = true;
this.cacheTTL = ttl;
}

public init(): void {
Expand Down Expand Up @@ -92,9 +99,11 @@ export default class LilTag {
const cachedEntry = cacheData[url];
if (!cachedEntry) return null;

const oneDay = 24 * 60 * 60 * 1000;
// Calculate TTL in milliseconds
const ttlInMilliseconds = this.cacheTTL * 1000;

if (Date.now() - cachedEntry.timestamp > oneDay) {
// Check if the cache has expired
if (Date.now() - cachedEntry.timestamp > ttlInMilliseconds) {
delete cacheData[url];
localStorage.setItem(LilTag.CACHE_KEY, JSON.stringify(cacheData));
return null;
Expand Down

0 comments on commit 3db5fea

Please sign in to comment.