Skip to content

Commit

Permalink
Merge pull request #8 from ndragun92/version/2.0.1
Browse files Browse the repository at this point in the history
update(docs): Add examples and update package version
  • Loading branch information
ndragun92 authored Mar 12, 2024
2 parents f4d00d3 + f339f28 commit de791b1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions _docs/content/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions _docs/content/3.examples/1.basic/1.adonis.md
Original file line number Diff line number Diff line change
@@ -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
```

::
2 changes: 2 additions & 0 deletions _docs/content/3.examples/1.basic/_dir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: 'Basic'
icon: fluent:style-guide-20-regular
10 changes: 10 additions & 0 deletions _docs/content/3.examples/2.advanced/1.adonis.md
Original file line number Diff line number Diff line change
@@ -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!
::
2 changes: 2 additions & 0 deletions _docs/content/3.examples/2.advanced/_dir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: 'Advanced'
icon: fluent:style-guide-20-regular
2 changes: 2 additions & 0 deletions _docs/content/3.examples/_dir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: 'Examples'
icon: fluent:style-guide-20-regular
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit de791b1

Please sign in to comment.