Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amemo v2 #3

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["amemo"]
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [2.0.0](https://github.com/enda-automation/amemo/compare/v1.2.2...v2.0.0) (2024-05-02)


### ⚠ BREAKING CHANGES

* changed import name

* changed import name ([30afd1a](https://github.com/enda-automation/amemo/commit/30afd1ac6f2ca5a649ec52c2168ae51e314349b4))

### [1.2.3](https://github.com/enda-automation/amemo/compare/v1.2.2...v1.2.3) (2024-04-08)

### [1.2.2](https://github.com/enda-automation/amemo/compare/v1.2.1...v1.2.2) (2024-04-08)
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# amemo

**amemo** is an experimental drop-in typesafe persistent memoization library.
**amemo** is an experimental drop-in, type safe, persistent (or not), zero-dependency memoization library.

It could be used to save time and resources by caching the results of expensive function calls, such as paid or rate limited API calls.

Expand All @@ -11,10 +11,10 @@ An in memory cache is also provided for non-persistent caching for environments
## Usage

```typescript
import {cacheProxy} from 'amemo';
import {amemo} from 'amemo';

const complexType = new ComplexType();
const memoizedType = cacheProxy(complexType); // drop-in replacement
const memoizedType = amemo(complexType); // drop-in replacement
memoizedType.nested.method({a: 1, b: 2}); // This will be memoized
memoizedType.nested.method({a: 1, b: 2}); // Free real estate
memoizedType.nested.method({a: 1}); // Not **memoized**
Expand Down Expand Up @@ -49,8 +49,8 @@ export type CacheProxyOpts = {
```typescript
export type FileCacheStoreOpts = {
// Location of the cache file
// Path will be recursilvely created if it doesn't exist
// Default: './cache.json'
// Path will be recursively created if it doesn't exist
// Default: './.amemo.json'
path?: string;

// If True the cache will be written to disk on every cache miss.
Expand Down Expand Up @@ -79,36 +79,36 @@ Writes to the cache file synchronously when autoSave is true. Otherwise, save()
#### autoSave

```typescript
import {cacheProxy} from 'amemo';
import {amemo, FileCacheStore} from 'amemo';

const cacheStore = new FileCacheStore({autoSave: false});
const complexType = new ComplexType();
const memoizedType = cacheProxy(complexType, {cacheStore}); // drop-in replacement
const memoizedType = amemo(complexType, {cacheStore}); // drop-in replacement
memoizedType.nested.method({a: 1, b: 2}); // This will be memoized
memoizedType.nested.method({a: 1, b: 2}); // Free real estate
memoizedType.nested.method({a: 1}); // Not **memoized**

// Save the cache to the disk
cacheStore.save(); // <-- Commit the cache to the disk
cacheStore.save(); // <-- Commit the cache to the disk, otherwise store will act like a in-memory cache
```

#### MemCacheStore

You can also use an in-memory for non persistent caching.

```typescript
import {cacheProxy, MemCacheStorage} from 'amemo';
import {amemo, MemCacheStorage} from 'amemo';

const cacheStore = new MemCacheStorage();
const complexType = new ComplexType();
const memoizedType = cacheProxy(complexType, {cacheStore}); // drop-in replacement
const memoizedType = amemo(complexType, {cacheStore}); // drop-in replacement
memoizedType.nested.method({a: 1, b: 2}); // This will be memoized
memoizedType.nested.method({a: 1, b: 2}); // Free real estate
```

### Alternative implementations

Alternative implementation, say browser compatible interfaces, can be implemented by implementing the CacheStore interface.
Alternative implementation, say a browser compatible interface like LocalStorage or IndexedDB , can be implemented by implementing the CacheStore interface.

```typescript
export interface CacheStore {
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ declare module "cache-proxy" {
pathExpire?: Record<string, number>;
cacheStore?: CacheStore;
};
export function cacheProxy<T extends object>(api: T, opts?: CacheProxyOpts): T;
type ProxyCache<T> = Record<string, T>;
export function createProxy<T extends object>(api: T, cache: CacheStore, opts: CacheProxyOpts, proxyCache: ProxyCache<T>, wrapperCache: Record<string, unknown>, path?: string): T;
}
declare module "amemo" {
import { CacheProxyOpts } from "cache-proxy";
export function amemo<T extends object>(api: T, opts?: CacheProxyOpts): T;
export * from "cache-store";
export * from "mem-cache-store";
export * from "file-cache-store";
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

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

Loading