npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cacheprovider

v1.0.0

Published

一个基于 React Context API 和 cache-manager 实现的缓存管理解决方案

Downloads

1

Readme

CacheContext

简介
CacheContext 是一个基于 React Context API 和 cache-manager 实现的缓存管理解决方案。它允许你在 React 应用中轻松地使用缓存功能,并提供了可配置的 TTL(Time To Live)和最大缓存数。

Introduction
CacheContext is a cache management solution based on React Context API and cache-manager. It allows you to easily use caching in your React application and provides configurable TTL (Time To Live) and maximum cache size.

安装 / Installation
bash
npm install cache-manager

使用 / Usage

  1. 在应用顶层提供 CacheContext / Provide CacheContext at the Top Level of the Application 在应用的顶层组件中使用 CacheProvider 提供 CacheContext。假设你的顶层组件是 App.tsx: Use CacheProvider to provide CacheContext at the top level of your application. Assuming your top-level component is App.tsx:

import React from 'react'; import ReactDOM from 'react-dom'; import { CacheProvider } from './CacheContext'; import App from './App';

ReactDOM.render( {/* 配置缓存的 TTL 为 600 秒,最大缓存数为 200 */} , document.getElementById('root') ); 3. 使用 CacheContext / Use CacheContext 在需要使用缓存功能的组件中使用 useCache 获取 cacheGet。例如: Use useCache to get cacheGet in components where you need caching functionality. For example: tsx Copy import React, { useState, useEffect } from 'react'; import { useCache } from './CacheContext';

const MyComponent = () => { const { cacheGet } = useCache(); const [data, setData] = useState(null);

useEffect(() => { const fetchData = async () => { const result = await cacheGet('myCacheKey', async () => { const response = await fetch('https://api.example.com/data'); return response.json(); }); setData(result); };

fetchData();

}, [cacheGet]);

if (!data) { return Loading...; }

return Data: {JSON.stringify(data)}; };

export default MyComponent;

API CacheProvider CacheProvider 组件用于提供缓存上下文。

Props children (ReactNode): 子组件。 ttl (number, optional): 缓存的 TTL(秒)。默认值为 300。 max (number, optional): 最大缓存数。默认值为 100。

useCache useCache 钩子用于获取缓存上下文中的 cacheGet 方法。 返回值 cacheGet (function): 用于获取缓存数据的方法。 cacheGet cacheGet 方法用于获取缓存数据,如果缓存中没有数据,则执行传入的函数并缓存结果。

参数 cacheKey (string): 缓存键。 func (function): 当缓存中没有数据时执行的函数。 Promise: 返回缓存数据或函数执行结果。 示例 / Example import React from 'react'; import ReactDOM from 'react-dom'; import { CacheProvider } from './CacheContext'; import App from './App';

ReactDOM.render( , document.getElementById('root') );

import React, { useState, useEffect } from 'react'; import { useCache } from './CacheContext';

const MyComponent = () => { const { cacheGet } = useCache(); const [data, setData] = useState(null);

useEffect(() => { const fetchData = async () => { const result = await cacheGet('myCacheKey', async () => { const response = await fetch('https://api.example.com/data'); return response.json(); }); setData(result); };

fetchData();

}, [cacheGet]);

if (!data) { return Loading...; }

return Data: {JSON.stringify(data)}; };

export default MyComponent;