Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hirotoshioi/query-cache
TanStack like caching library
https://github.com/hirotoshioi/query-cache
Last synced: about 1 month ago
JSON representation
TanStack like caching library
- Host: GitHub
- URL: https://github.com/hirotoshioi/query-cache
- Owner: HirotoShioi
- Created: 2024-11-28T02:13:41.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-11-28T05:16:20.000Z (about 1 month ago)
- Last Synced: 2024-11-28T05:25:55.583Z (about 1 month ago)
- Language: TypeScript
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# query-cache
`query-cache` is a lightweight library that provides simple yet powerful cache management.
Inspired by the [`tanstack query`](https://github.com/TanStack/query), it offers a flexible and straightforward way to handle caching.## Features
- **High-level and intuitive interface**
Manage data fetching with just a cache key and a fetch function.```typescript
const data = await query.cache({ queryKey: ['key'], queryFn: async () => fetchData() });
```- **Zero dependencies and lightweight**
Built with no external dependencies, making it highly performant and lightweight.- **Compatible with both browser and server environments**
Designed to work seamlessly in both Node.js and browser contexts.- **TypeScript support for type safety**
Fully typed API to ensure safe and efficient development.- **Flexible cache management**
Easily configure expiration times and stale data behavior.```typescript
await query.cache({ queryKey: ['key'], queryFn: fetchData, staleTime: 3000 });
```---
## Use Cases
- **Caching API responses**
Reduce network load and enable faster data retrieval.
- **Unified data fetching management**
Avoid duplicate fetches and streamline data handling.
- **Efficient data storage for browser and server**
Ideal for temporary data storage and cache management.---
## Installation
Install using one of the following commands:
```bash
npm install query-cache
# or
yarn add query-cache
# or
pnpm add query-cache
```---
## Quick Start
Here’s a basic example of how to use `query-cache`:
### Creating a Simple Cache
```typescript
import { QueryCache } from 'query-cache';// Create a QueryCache instance
const query = new QueryCache();// Fetch or retrieve cached data
const data = await query.cache({
queryKey: ['example-key'], // Cache key
queryFn: async () => {
// Your data-fetching logic
return 'Hello, query-cache!';
},
});console.log(data); // 'Hello, query-cache!'
```### Invalidating and Refetching Cache
You can invalidate cached data and refetch it if needed.
```typescript
const query = new QueryCache();// Fetch and cache data
await query.cache({
queryKey: ['invalidate-example'],
queryFn: async () => 'First Fetch',
});// Invalidate cache
await query.invalidateCache({
queryKey: ['invalidate-example'],
refetch: true, // Refetch after invalidation
});
```---
## Tests
This project includes comprehensive tests to ensure reliability.
```bash
npm run test
```---
## Contributing
`query-cache` is open-source, and contributions are welcome! Please follow these guidelines when submitting a pull request:
1. Follow the coding style (Prettier, ESLint).
2. Add necessary test cases.---
## License
MIT License © Hiroto Shioi