Skip to content

Commit

Permalink
refactor & fix: lint issues and simplify parseExpTime
Browse files Browse the repository at this point in the history
Fix linting issues with explicit returns and types and simplify parseExpTime function
  • Loading branch information
unkn0wn-root committed Jan 23, 2025
1 parent 3c6ab75 commit 93c3490
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
with:
deno-version: canary

- name: Run lint
run: deno lint **/*.ts

- name: Run tests
run: deno test --coverage=./src/tests/coverage_report ./src/tests/ --allow-net --allow-import

Expand Down
22 changes: 11 additions & 11 deletions src/doublyLinkedLists.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { cacheValue } from './types.ts'
import type { CacheValue } from './types.ts'

/**
* Class definition for linked list containing cached values for both LRU and LFU.
*/
export class Node {
next: Node | null;
prev: Node | null;
value: cacheValue;
value: CacheValue;
key: string;
count: number;
byteSize: number;
Expand All @@ -15,7 +15,7 @@ export class Node {

constructor(
key: string,
value: cacheValue,
value: CacheValue,
byteSize: number,
timeStamp: Date,
parent?: FreqNode
Expand All @@ -42,11 +42,11 @@ export class ValueDoublyLinkedList {

public addHead(
key: string,
value: cacheValue,
value: CacheValue,
byteSize: number,
timeStamp: Date,
parent?: FreqNode
) {
): Node {
const node = new Node(key, value, byteSize, timeStamp, parent);
if (!this.head) {
this.head = node;
Expand All @@ -59,7 +59,7 @@ export class ValueDoublyLinkedList {
return this.head;
}

public delete(node: Node | null) {
public delete(node: Node | null): Node | undefined {
if (!node) {
return;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ export class FreqDoublyLinkedList {
this.tail = null;
}

public addNewFreq(key: string, value: cacheValue, byteSize: number, timeStamp: Date) {
public addNewFreq(key: string, value: CacheValue, byteSize: number, timeStamp: Date): Node {
if (!this.head) {
this.head = new FreqNode(1);
this.tail = this.head;
Expand All @@ -146,7 +146,7 @@ export class FreqDoublyLinkedList {
return this.head.valList.addHead(key, value, byteSize, timeStamp, this.head);
}

public increaseFreq(node: Node) {
public increaseFreq(node: Node): Node | undefined {
if (!node.parent) {
return;
}
Expand Down Expand Up @@ -175,11 +175,11 @@ export class FreqDoublyLinkedList {
return parent.next.valList.addHead(key, value, byteSize, timeStamp, parent.next);
}

public deleteLeastFreq = () => this.head ?
public deleteLeastFreq = (): Node | undefined => this.head ?
this.deleteValNode(this.head.valList.tail)
: undefined;

public deleteValNode(node: Node | null) {
public deleteValNode(node: Node | null): Node | undefined {
if (!node || !node.parent) {
return;
}
Expand All @@ -193,7 +193,7 @@ export class FreqDoublyLinkedList {
return node;
}

public delete(freqNode: FreqNode | null) {
public delete(freqNode: FreqNode | null): FreqNode | undefined {
if (!freqNode) {
return;
}
Expand Down
23 changes: 14 additions & 9 deletions src/lfu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FreqDoublyLinkedList, Node } from './doublyLinkedLists.ts'
import { cacheValue } from './types.ts'
import PerfMetrics from './performanceMetrics.ts'
import { FreqDoublyLinkedList, type Node } from './doublyLinkedLists.ts'
import type { CacheValue } from './types.ts'
import type PerfMetrics from './performanceMetrics.ts'


/**
Expand Down Expand Up @@ -31,7 +31,7 @@ class LFU {
* @param value
* @returns
*/
public put(key: string, value: cacheValue, byteSize: number) {
public put(key: string, value: CacheValue, byteSize: number): CacheValue | undefined {
if (this.cache[key]){
this.metrics.decreaseBytes(this.cache[key].byteSize);
this.metrics.increaseBytes(byteSize);
Expand All @@ -57,8 +57,11 @@ class LFU {
this.metrics.decreaseBytes(deletedNode.byteSize);
}

public get(key: string) {
if (!this.cache[key]) return;
public get(key: string): CacheValue | undefined {
if (!this.cache[key]) {
return;
}

//if entry is stale, deletes and exits
const currentTime = new Date();
const timeElapsed = Math.abs(currentTime.getTime() - this.cache[key].timeStamp.getTime()) / 1000;
Expand All @@ -76,9 +79,11 @@ class LFU {
}
}

public delete(key: string) {
public delete(key: string): Node | undefined {
const node = this.cache[key];
if (!node) return;
if (!node) {
return;
}

this.freqList.deleteValNode(node);

Expand All @@ -88,7 +93,7 @@ class LFU {
return node;
}

public clear() {
public clear(): void {
this.freqList = new FreqDoublyLinkedList();
this.cache = {};
this.length = 0;
Expand Down
22 changes: 13 additions & 9 deletions src/lru.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ValueDoublyLinkedList, Node } from './doublyLinkedLists.ts'
import { cacheValue } from './types.ts'
import PerfMetrics from './performanceMetrics.ts'
import { ValueDoublyLinkedList, type Node } from './doublyLinkedLists.ts'
import type { CacheValue } from './types.ts'
import type PerfMetrics from './performanceMetrics.ts'

/**
* Cache implementing a least recently used eviction policy.
Expand Down Expand Up @@ -33,7 +33,7 @@ class LRU {
* @param value
* @returns
*/
public put(key: string, value: cacheValue, byteSize: number) {
public put(key: string, value: CacheValue, byteSize: number): CacheValue | undefined {
//if key alreadys exits in cache, replace key value with new value, and move to list head.
if (this.cache[key]){
this.metrics.decreaseBytes(this.cache[key].byteSize);
Expand Down Expand Up @@ -67,9 +67,11 @@ class LRU {
* @param key
* @returns
*/
public get(key: string) {
public get(key: string): CacheValue | undefined {
//If no matching cache value (cache miss), return next();
if (!this.cache[key]) return undefined;
if (!this.cache[key]) {
return;
}

//if entry is stale, deletes and exits
const currentTime = new Date();
Expand Down Expand Up @@ -100,9 +102,11 @@ class LRU {
* @param key
* @returns
*/
public delete(key: string) {
public delete(key: string): Node | undefined {
const node = this.cache[key];
if (!node) return;
if (!node) {
return;
}

this.list.delete(node);
delete this.cache[key];
Expand All @@ -114,7 +118,7 @@ class LRU {
/**
* Clears entire cache contents.
*/
public clear() {
public clear(): void {
this.list = new ValueDoublyLinkedList();
this.cache = {};
this.length = 0;
Expand Down
16 changes: 8 additions & 8 deletions src/performanceMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class PerfMetrics {
this.hitLatencyTotal = 0;
}

public addEntry = () => this.numberOfEntries++;
public deleteEntry = () => this.numberOfEntries--;
public readProcessed = () => this.readsProcessed++;
public writeProcessed = () => this.writesProcessed++;
public clearEntires = () => this.numberOfEntries = 0;
public increaseBytes = (bytes: number) => this.memoryUsed += bytes;
public decreaseBytes = (bytes: number) => this.memoryUsed -= bytes;
public updateLatency = (latency: number, hitOrMiss: 'hit' | 'miss') => {
public addEntry = (): number => this.numberOfEntries++;
public deleteEntry = (): number => this.numberOfEntries--;
public readProcessed = (): number => this.readsProcessed++;
public writeProcessed = (): number => this.writesProcessed++;
public clearEntires = (): number => this.numberOfEntries = 0;
public increaseBytes = (bytes: number): number => this.memoryUsed += bytes;
public decreaseBytes = (bytes: number): number => this.memoryUsed -= bytes;
public updateLatency = (latency: number, hitOrMiss: 'hit' | 'miss'): void => {
if (hitOrMiss === 'hit'){
this.hitLatencyTotal += latency;
this.currentHitLatency = latency;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/performanceMetrics_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
assertEquals,
assertInstanceOf,
Context
type Context
} from "../../deps.ts";
import Zoic from '../../zoic.ts';
import PerfMetrics from '../performanceMetrics.ts';
Expand Down
8 changes: 4 additions & 4 deletions src/tests/zoic_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assertEquals,
assertInstanceOf,
assertRejects,
Context
type Context
} from "../../deps.ts";
import Zoic from "../../zoic.ts";
import LRU from "../lru.ts";
Expand Down Expand Up @@ -78,7 +78,7 @@ Deno.test(
cache: "LRU",
}),
TypeError,
"Cache expiration time must be string formatted as a numerical value followed by 'd', 'h', 'm', or 's', or a number representing time in seconds.",
'Invalid format. Use number followed by d, h, m, or s (e.g., "1d,12h").',
);
},
);
Expand Down Expand Up @@ -112,12 +112,12 @@ Deno.test(
assertThrows(
() => new Zoic({ expire: 31536001 }),
TypeError,
"Cache expiration time out of range.",
"Cache expiration must be between 1 second and 1 year.",
);
assertThrows(
() => new Zoic({ expire: 0 }),
TypeError,
"Cache expiration time out of range.",
"Cache expiration must be between 1 second and 1 year.",
);
},
);
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface options {
export interface Options {
cache?: string;
port?: number;
hostname?: string;
Expand All @@ -7,8 +7,8 @@ export interface options {
capacity?: number;
}

export interface cacheValue {
export interface CacheValue {
headers: { [k:string]:string };
body: Uint8Array;
status: number;
}
}
Loading

0 comments on commit 93c3490

Please sign in to comment.