Skip to content

Commit

Permalink
Merge pull request #1110 from dm3-org/fixTypoPersistAncePersistEnce
Browse files Browse the repository at this point in the history
Fix typo persistAnce persistEnce
  • Loading branch information
AlexNi245 authored Jul 18, 2024
2 parents 0136ce1 + 0848699 commit 96b3ddb
Show file tree
Hide file tree
Showing 37 changed files with 53 additions and 53 deletions.
16 changes: 8 additions & 8 deletions packages/lib/shared/src/cache/Web3ProviderCacheFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { ethers } from 'ethers';
import { ICache } from './impl/ICache';
import { LRUCache } from './impl/LRUCache';
import { TTLCache, TTLCacheItem } from './impl/TTLCache';
import { IPersistance } from './persistance/IPersistance';
import { InMemory } from './persistance/InMemory';
import { IPersistance } from './persistence/IPersistance';
import { InMemory } from './persistence/InMemory';
import { sha256 } from '../sha256';
import { LocalStorage } from './persistance/LocalStorage';
import { LocalStorage } from './persistence/LocalStorage';

const DEFAULT_CAPACITY = 500;
//1 hour
Expand All @@ -18,16 +18,16 @@ export class Web3ProviderCacheFactory {
constructor(provider: ethers.providers.JsonRpcProvider) {
this.provider = provider;
}
//TTL cache with local storage as persistance
//TTL cache with local storage as persistence
public TTLLocalStorage<T>(ttl: number = DEFAULT_TTL) {
return this.TTL(new LocalStorage<TTLCacheItem<T>>(), ttl);
}
//Returns an instance of the web3 provder. Requests are cached for a given time to live
public TTL<T>(
persistance: IPersistance<TTLCacheItem<T>> = new InMemory(),
persistence: IPersistance<TTLCacheItem<T>> = new InMemory(),
ttl: number = DEFAULT_TTL,
): ethers.providers.JsonRpcProvider {
const cache = new TTLCache<T>(DEFAULT_CAPACITY, ttl, persistance);
const cache = new TTLCache<T>(DEFAULT_CAPACITY, ttl, persistence);
const instance = Web3ProviderCacheFactory._createInstance(
this.provider,
cache,
Expand All @@ -36,10 +36,10 @@ export class Web3ProviderCacheFactory {
}
//Returns an instance of the web3 provider. Requests are cached using LRU strategy
public LRU<T>(
persistance: IPersistance<T> = new InMemory(),
persistence: IPersistance<T> = new InMemory(),
capacity: number = DEFAULT_CAPACITY,
): ethers.providers.JsonRpcProvider {
const cache = new LRUCache<T>(capacity, persistance);
const cache = new LRUCache<T>(capacity, persistence);
const instance = Web3ProviderCacheFactory._createInstance(
this.provider,
cache,
Expand Down
36 changes: 18 additions & 18 deletions packages/lib/shared/src/cache/impl/LRUCache.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
//Simple cache using LRU as a cache strategy to keep the most recent values

import { IPersistance } from '../persistance/IPersistance';
import { InMemory } from '../persistance/InMemory';
import { IPersistance } from '../persistence/IPersistance';
import { InMemory } from '../persistence/InMemory';
import { ICache } from './ICache';

//Thanks to Gashawk.io for the implementation
export class LRUCache<T> implements ICache<T> {
private capacity: number;
private persistance: IPersistance<T>;
private persistence: IPersistance<T>;

constructor(
capacity: number,
persistance: IPersistance<T> = new InMemory(),
persistence: IPersistance<T> = new InMemory(),
) {
this.capacity = capacity;
this.persistance = persistance;
this.persistence = persistence;
}

get(key: string): T | undefined {
if (!this.persistance.has(key)) {
if (!this.persistence.has(key)) {
return undefined;
}
const value = this.persistance.get(key)!;
const value = this.persistence.get(key)!;
// Remove the key and re-insert it to update its position (most recently used)
this.persistance.delete(key);
this.persistance.set(key, value);
this.persistence.delete(key);
this.persistence.set(key, value);
return value;
}

set(key: string, value: T): void {
if (this.persistance.has(key)) {
if (this.persistence.has(key)) {
// Remove the key to update its position (most recently used)
this.persistance.delete(key);
} else if (this.persistance.size() === this.capacity) {
this.persistence.delete(key);
} else if (this.persistence.size() === this.capacity) {
// Remove the least recently used (first) entry
const firstKey = this.persistance.keys().next().value;
this.persistance.delete(firstKey);
const firstKey = this.persistence.keys().next().value;
this.persistence.delete(firstKey);
}
this.persistance.set(key, value);
this.persistence.set(key, value);
}

has(key: string): boolean {
return this.persistance.has(key);
return this.persistence.has(key);
}

length(): number {
return this.persistance.size();
return this.persistence.size();
}

clear(): void {
this.persistance.clear();
this.persistence.clear();
}
}
6 changes: 3 additions & 3 deletions packages/lib/shared/src/cache/impl/TTLCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPersistance } from '../persistance/IPersistance';
import { IPersistance } from '../persistence/IPersistance';
import { ICache } from './ICache';

export type TTLCacheItem<T> = {
Expand All @@ -15,11 +15,11 @@ export class TTLCache<T> implements ICache<T> {
constructor(
capacity: number,
ttl: number,
persistance: IPersistance<TTLCacheItem<T>>,
persistence: IPersistance<TTLCacheItem<T>>,
) {
this.capacity = capacity;
this.ttl = ttl;
this.persitance = persistance;
this.persitance = persistence;
}

get(key: string): T | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Decouple the cache from the persistance layer
//Decouple the cache from the persistence layer
export interface IPersistance<T> {
has(key: string): boolean;
get(key: string): T | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { IPersistance } from './IPersistance';
//Simply store items in Memory
export class InMemory<T> implements IPersistance<T> {
private readonly persistance: Map<string, T>;
private readonly persistence: Map<string, T>;

constructor(persistance: Map<string, T> = new Map()) {
this.persistance = persistance;
constructor(persistence: Map<string, T> = new Map()) {
this.persistence = persistence;
}
has(key: string): boolean {
return this.persistance.has(key);
return this.persistence.has(key);
}
keys(): IterableIterator<string> {
return this.persistance.keys();
return this.persistence.keys();
}

get(key: string): T | null {
return this.persistance.get(key) || null;
return this.persistence.get(key) || null;
}

set(key: string, value: T): void {
this.persistance.set(key, value);
this.persistence.set(key, value);
}

delete(key: string): void {
this.persistance.delete(key);
this.persistence.delete(key);
}

size(): number {
return this.persistance.size;
return this.persistence.size;
}

clear(): void {
this.persistance.clear();
this.persistence.clear();
}
serialize(): string {
return JSON.stringify(Array.from(this.persistance.entries()));
return JSON.stringify(Array.from(this.persistence.entries()));
}

static fromJson<T>(json: string): InMemory<T> {
Expand Down
4 changes: 2 additions & 2 deletions packages/offchain-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"docker:up": "docker compose up -d",
"test": "yarn docker:up && yarn prisma:migrate && yarn mocha --require ts-node/register --extension .test.ts --recursive ./src",
"start": "yarn prisma:migrate && yarn ts-node --transpile-only ./src/index.ts",
"build": "yarn prisma generate --schema=./src/persistance/schema.prisma && yarn tsc",
"prisma:migrate": "yarn prisma migrate dev --name init --schema src/persistance/schema.prisma"
"build": "yarn prisma generate --schema=./src/persistence/schema.prisma && yarn tsc",
"prisma:migrate": "yarn prisma migrate dev --name init --schema src/persistence/schema.prisma"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers';
import { IDatabase } from '../../../persistance/IDatabase';
import { IDatabase } from '../../../persistence/IDatabase';
import { interceptAddr } from './intercept';
import { logDebug } from '@dm3-org/dm3-lib-shared';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDatabase } from '../../../persistance/IDatabase';
import { IDatabase } from '../../../persistence/IDatabase';
import { PROFILE_RECORD_NAME } from '@dm3-org/dm3-lib-profile';
import { stringify } from '@dm3-org/dm3-lib-shared';
import { interceptTextRecord } from './intercept';
Expand Down
6 changes: 3 additions & 3 deletions packages/offchain-resolver/src/http/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import express from 'express';

import request from 'supertest';
import winston from 'winston';
import { getDatabase, getDbClient } from '../persistance/getDatabase';
import { IDatabase } from '../persistance/IDatabase';
import { getDatabase, getDbClient } from '../persistence/getDatabase';
import { IDatabase } from '../persistence/IDatabase';
import { profile } from './profile';

import { PrismaClient } from '@prisma/client';
import * as dotenv from 'dotenv';
import { clearDb } from '../persistance/clearDb';
import { clearDb } from '../persistence/clearDb';

import { expect } from 'chai';
import { sign } from '@dm3-org/dm3-lib-crypto';
Expand Down
6 changes: 3 additions & 3 deletions packages/offchain-resolver/src/http/resolverEndpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { expect } from 'chai';
import { ethers } from 'ethers';
import express from 'express';
import request from 'supertest';
import { IDatabase } from '../persistance/IDatabase';
import { clearDb } from '../persistance/clearDb';
import { getDatabase, getDbClient } from '../persistance/getDatabase';
import { IDatabase } from '../persistence/IDatabase';
import { clearDb } from '../persistence/clearDb';
import { getDatabase, getDbClient } from '../persistence/getDatabase';
import { encodeEnsName } from './handleCcipRequest/dns/encodeEnsName';
import { Interceptor } from './handleCcipRequest/handler/intercept';
import { profile } from './profile';
Expand Down
2 changes: 1 addition & 1 deletion packages/offchain-resolver/src/http/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers';
import { IDatabase } from '../persistance/IDatabase';
import { IDatabase } from '../persistence/IDatabase';

export interface WithLocals {
locals: Record<string, any> &
Expand Down
2 changes: 1 addition & 1 deletion packages/offchain-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as dotenv from 'dotenv';
import express from 'express';
import http from 'http';
import { resolverEndpoint } from './http/resolverEndpoint';
import { getDatabase } from './persistance/getDatabase';
import { getDatabase } from './persistence/getDatabase';
import { getWeb3Provider } from './utils/getWeb3Provider';

import { profile } from './http/profile';
Expand Down

0 comments on commit 96b3ddb

Please sign in to comment.