Skip to content

Commit

Permalink
fixed bugs on ubuntu ,fixed crossplatform issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniTurtle committed May 8, 2024
1 parent 79c3d60 commit 63af40d
Show file tree
Hide file tree
Showing 87 changed files with 4,898 additions and 50 deletions.
9 changes: 9 additions & 0 deletions luaLibs/cache/.lua-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
break_after_table_lb: true
break_before_table_rb: false
break_before_functioncall_rp: true
break_before_functiondef_rp: true
chop_down_table: true
extra_sep_at_table_end: true
keep_simple_control_block_one_line: false
keep_simple_function_one_line: false
column_table_limit: 1
13 changes: 13 additions & 0 deletions luaLibs/cache/.luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
std = 'max'
include_files = {
'cache.lua',
'lib/***.lua',
'test/*_test.lua',
}
ignore = {
'assert',
-- unused argument
'212',
-- line is too long.
'631',
}
7 changes: 7 additions & 0 deletions luaLibs/cache/.luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
codefromstrings = false
runreport = true
deletestats = false
modules = {
['cache'] = 'cache.lua',
['cache.inmem'] = 'lib/inmem.lua',
}
168 changes: 168 additions & 0 deletions luaLibs/cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# lua-cache

[![test](https://github.com/mah0x211/lua-cache/actions/workflows/test.yml/badge.svg)](https://github.com/mah0x211/lua-cache/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/mah0x211/lua-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/mah0x211/lua-cache)

pluggable cache storage module.

---

## Installation

```sh
luarocks install cache
```

## Usage

```lua
local sleep = require('nanosleep.sleep')

-- `cache.inmem` is built-in module
-- this module uses the lua table as in-memory cache storage.
local cache = require('cache.inmem')
-- default ttl: 2 seconds
local c = cache.new(2)
local key = 'test'
local val = 'test val'

print(c:set(key, val)) -- true
print(c:get(key)) -- 'test val'
print(c:delete(key)) -- true

print(c:set(key, val)) -- true
-- after 2 seconds
sleep(2)
print(c:get(key)) -- nil
```


## c = cache.new( store, ttl )

create an instance of cache.

**Parameters**

- `store:table|userdata`: store must be implemented the following methods;
- `set`, `get`, `delete`, `rename`, `keys` and `evict`.
- `ttl:integer`: default expiration seconds.

**Returns**

- `c:cache`: instance of `cache`.


## ok, err = cache:set( key, val [, ttl] )

set a key-value pair.
this method calls the `store:set()` method after validating its arguments.

**Parameters**

- `key:string`: a string that matched to the pattern `^[a-zA-Z0-9_%-]+$'`.
- `val:any`: any value except `nil`. this value will be encoded into JSON string.
- `ttl:integer`: expiration seconds greater or equal to `0`. (optional)

**Returns**

- `ok:boolean`: `true` on success, or `false` on failure.
- `err:any`: error message.


## val, err = cache:get( key [, ttl] )

get a value associated with a `key` and update an expiration seconds if `ttl` is specified.
this method calls the `store:get()` method after validating its arguments.

**Parameters**

- `key:string`: a string that matched to the pattern `^[a-zA-Z0-9_%-]+$'`.
- `ttl:integer`: update an expiration seconds. (optional)

**Returns**

- `val:any`: a value.
- `err:any`: error message.


## ok, err = cache:delete( key )

delete a value associated with a `key`.
this method calls the `store:delete()` method after validating its arguments.

**Parameters**

- `key:string`: a string that matched to the pattern `^[a-zA-Z0-9_%-]+$'`.

**Returns**

- `ok:boolean`: `true` on success, or `false` on failure.
- `err:any`: error message.


## ok, err = cache:rename( oldkey, newkey )

rename the `oldkey` name to `newkey`.
this method calls the `store:rename()` method after validating its arguments.

**Parameters**

- `oldkey:string`: a string that matched to the pattern `^[a-zA-Z0-9_%-]+$'`.
- `newkey:string`: a string that matched to the pattern `^[a-zA-Z0-9_%-]+$'`.

**Returns**

- `ok:boolean`: `true` on success, or `false` on failure.
- `err:any`: error message.


## ok, err = cache:keys( callback, ... )

execute a provided function once for each key. it is aborted if it returns `false` or an error.
this method calls the `store:keys()` method after validating its arguments.

**Parameters**

- `callback:function`: a function that called with each key.
```
ok, err = callback(key)
- ok:boolean: true on continue.
- err:any: an error message.
- key:string: cached key string.
```

**Returns**

- `ok:boolean`: `true` on success, or `false` on failure.
- `err:any`: error message.



## n, err = cache:evict( callback [, n, ...] )

execute a provided function once before key is deleted. it is aborted if it returns `false` or an error.
this method calls the `store:evict()` method after validating its arguments.

**Parameters**

- `callback:function`: a function that called with key.
```
ok, err = callback(key)
- ok:boolean: true on continue.
- err:any: an error message.
- key:string: cached key string.
```
- `n:integer`: maximum number of keys to be evicted.

**Returns**

- `n:integer`: number of keys evicted.
- `err:any`: error message.



## Note

`cache` module is an interface implementation of the storage plugins.
if you need to create original plugins, please refer to the source of `lib/inmem.lua`.

Loading

0 comments on commit 63af40d

Please sign in to comment.