diff --git a/README.md b/README.md index 3377b01..d60a857 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ File-Sys-Cache is an npm package that provides a simple and efficient system fil - **Flush Cache by Regex**: Flush cache entries that match a given regular expression. - **Flush Whole Cache**: Clear the entire cache, removing all stored entries. - **Monitoring**: Monitor cache usage and performance statistics, including invalidated files count, logs over time, size over time, and request count. +- **TypeScript**: Full TypeScript support ## Installation diff --git a/_docs/content/0.index.md b/_docs/content/0.index.md index 08293c3..1665d96 100644 --- a/_docs/content/0.index.md +++ b/_docs/content/0.index.md @@ -32,6 +32,7 @@ File-Sys-Cache is an npm package that provides a simple and efficient system fil - **Flush Cache by Regex** - **Flush Whole Cache** - **Monitoring** +- **TypeScript Support** :: #support diff --git a/_docs/content/3.examples/1.basic/1.adonis.md b/_docs/content/3.examples/1.basic/1.adonis.md new file mode 100644 index 0000000..9526c46 --- /dev/null +++ b/_docs/content/3.examples/1.basic/1.adonis.md @@ -0,0 +1,113 @@ +# Adonis.js + +:button-link[Adonis.js Documentation]{icon="tabler:brand-adonis-js" href="https://adonisjs.com" blank} +--- + +### Basic Example + +::code-group + +```ts [todos_controller.ts] +import { HttpContext } from '@adonisjs/core/http' +import cache from '#services/cache.service' +import Todo from '#models/todo' + +export default class TodosController { + /** + * Return list of all todos + */ + async index({ response }: HttpContext) { + let cachedData; + const fileName = 'todos' + const key = 'index' + + try { + cachedData = await cache.get({ fileName, key }) + } catch (_) {} + + if (!cachedData) { + try { + cachedData = await Todo.all() + await cache.set({ fileName, key, payload: cachedData }) + } catch (e) { + return response.badRequest({ error, message: 'Something went wrong while getting records' }) + } + } + return response.ok({ data: cachedData }) + } + + /** + * Handle form submission to create a new todo + */ + async store({ request, response }: HttpContext) { + const { name } = request.only(['name']) + + try { + await Todo.create({ + name, + completed: false, + }) + await cache.flushByRegex('todos') // Invalidates todos so that we get fresh results on next index() call + return response.ok({ data: 'Successfully inserted a new record' }) + } catch (error) { + return response.badRequest({ error, message: 'Something went wrong while inserting a record' }) + } + } + + /** + * Display a single todo by id. + */ + async show({ request, response }: HttpContext) { + const id = request.param('id') + + let cachedData; + const fileName = 'todos-id' + const key = id + + try { + cachedData = await cache.get({ fileName, key }) + } catch (_) {} + + if (!cachedData) { + try { + cachedData = await Todo.find(id) + await cache.set({ fileName, key, payload: cachedData }) + } catch (error) { + return response.badRequest({ error, message: 'Something went wrong while getting a record' }) + } + } + return response.ok({ data: cachedData }) + } + + /** + * Handle the form submission to update a specific todo by id + */ + async update({ request }: HttpContext) { + const id = request.param('id') + const { name } = request.only(['name']) + + try { + const todo = await Todo.findOrFail(id) + todo.name = name + + await todo.save() + await cache.flushByRegex('todos', id) // Invalidates single todo by id so that we get fresh results on next show() call + return response.ok({ data: 'Successfully updated a record' }) + } catch (error) { + return response.badRequest({ error, message: 'Something went wrong while updating a record' }) + } + } +} +``` + +```ts [cache.service.ts] +import { FileSysCache } from 'file-sys-cache' + +const cache = new FileSysCache({ + basePath: './.file-sys-cache' +}) + +export default cache +``` + +:: \ No newline at end of file diff --git a/_docs/content/3.examples/1.basic/_dir.yml b/_docs/content/3.examples/1.basic/_dir.yml new file mode 100644 index 0000000..8e20722 --- /dev/null +++ b/_docs/content/3.examples/1.basic/_dir.yml @@ -0,0 +1,2 @@ +title: 'Basic' +icon: fluent:style-guide-20-regular diff --git a/_docs/content/3.examples/2.advanced/1.adonis.md b/_docs/content/3.examples/2.advanced/1.adonis.md new file mode 100644 index 0000000..dd119f2 --- /dev/null +++ b/_docs/content/3.examples/2.advanced/1.adonis.md @@ -0,0 +1,10 @@ +# Adonis.js + +:button-link[Adonis.js Documentation]{icon="tabler:brand-adonis-js" href="https://adonisjs.com" blank} +--- + +### Advanced Example + +::alert{type="info"} +Coming soon! +:: \ No newline at end of file diff --git a/_docs/content/3.examples/2.advanced/_dir.yml b/_docs/content/3.examples/2.advanced/_dir.yml new file mode 100644 index 0000000..fcef25e --- /dev/null +++ b/_docs/content/3.examples/2.advanced/_dir.yml @@ -0,0 +1,2 @@ +title: 'Advanced' +icon: fluent:style-guide-20-regular diff --git a/_docs/content/3.examples/_dir.yml b/_docs/content/3.examples/_dir.yml new file mode 100644 index 0000000..7daf7c2 --- /dev/null +++ b/_docs/content/3.examples/_dir.yml @@ -0,0 +1,2 @@ +title: 'Examples' +icon: fluent:style-guide-20-regular diff --git a/package.json b/package.json index c0dd6d1..f6e9912 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "file-sys-cache", - "version": "2.0.0", + "version": "2.0.1", "description": "A Node.js package providing efficient caching using the file system for storage.", "type": "module", "main": "./dist/file-sys-cache.cjs",