open-tiny-orm

一个 OpenResty 的小型 ORM

$ opm get yfge/open-tiny-orm

orm

OpenResty 的一个 ORM

更新记录

v0.3 2019 年 4 月 8 日

  1. 加入了 util.cfg 以依赖注入的形式管理配置

  1. nginx 变量管理默认配置路径

  1. 加入缓存机制

  1. orm.mysql.factory 支持默认缓存

v0.21 2019 年 3 月 26 日

初始发布

目录

@[TOC]

安装

推荐使用 opm 进行安装。

    opm install yfge/open-tiny-orm

设置

依赖注入及约定

open-tiny-orm 集成了 orm、缓存等功能,为了方便使用和集成,这些组件在初始化时可以通过自定义的配置传入(传入 table),也可以通过依赖注入的形式传入配置文件中的配置 key。并且这种配置是多层有效的。如,当使用一个 redis 的缓存时,可以直接简单的应用如下:

  1. 直接初始化缓存

        local cacheFac = require('tiny.cache')
        local config = {
            cache_type="redis",
            cache_cfg={
                key_pre="",
                expired=1000,
                redis={
                    timeout=1000,
                    pool = {
                        maxIdelTime = 129999,
                        size=200
                    },
                    clusters={
                        {   "127.0.0.1",6379 }
                    },
                    password="Pa88word",
                    database=1,
                }
            },
        }
        local cache = cacheFac.get(config)

也可以在 config/redis.lua 下定义 redis.lua 的配置

  1. 定义 config/redis.lua 后初始化缓存 config/redis.lua

    `lua return { default = { timeout=1000, pool = { maxIdelTime = 129999, size=200 }, clusters={ { "127.0.0.1",6379 } }, password="Pa88word", database=1, } } ` 之后在缓存配置中直接引入 redis 的配置:

    `lua local cacheFac = require('tiny.cache') local config = { cache_type="redis", cache_cfg={ expired=1000, key_pre="", redis="default", }, } local cache = cacheFac.get(config) ` 或是直接将这个配置存在 config/cache.lua 中:

  2. 定义 config/cache.lua 后初始化缓存 config/cache.lua

    `lua return { rediscache = { cache_type="redis", cache_cfg={ expired=1000, key_pre="", redis="default" } } } `

这时业务代码变为:

    ```lua
    local cacheFac = require('tiny.cache')
    local cache = cacheFac.get('rediscache')
    ```

配置文件的位置

所有的配置都以配置文件的形式存放,当可以通过 nginx 变量来更改配置文件的文件。

  • open\_tiny\_mysql mysql 配置文件,默认 config.mysql

  • open\_tiny\_redis redis 配置文件,默认 config.redis

  • open\_tiny\_cache 缓存配置文件,默认 config.cache

        server {
                set $open_tiny_mysql "config.mysql";
                set $open_tiny_redis "config.redis";
                set $open_tiny_cache "config.cache";
                listen 8080;
                location  / {
                    content_by_lua_file src/content_by_lua8080.lua;
                    log_by_lua_file src/log_by_lua.lua;
                    }
                }

配置文件的格式

  1. config.mysql

    `lua return { default = { timeout = 3000, pool = { maxIdleTime = 120000, size = 800, }, clusters = { master = {"127.0.0.1", "3306"}, slave = { {"127.0.0.1", "3306"}, } }, database = "open_tiny", user = "tiny", password = "Pa88word", charset = "utf8", maxPacketSize = 1024*1024, } } `

  1. config.redis

    `lua return { default = { timeout=1000, pool = { maxIdelTime = 129999, size=200 }, clusters={ { "127.0.0.1",6379 } }, password="Pa88word", database=1, } } `

  1. config.cache

    `lua return { redis = { cache_type="redis", cache_cfg={ catlog="user", expired=1000, redis="default" } }, user = { cache_type="shared", cache_cfg={ catlog="user", expired=10 } }, sync = { cache_type = "sync", cache_cfg ={ redis="default", catlog="user", expired=1000, channel="lua:sync:cache", } }, sync_redis ={ cache_type = "sync", cache_cfg ={ redis={ timeout=1000, pool = { maxIdelTime = 129999, size=200 }, clusters={ { "127.0.0.1",6379 } }, password="Pa88word", }, catlog="user", expired=1000, channel="lua:sync:cache", } } } `

快速开始

定义 model

  • 用现有的配置定义,配置字段的含义参考配置一节

        local model = require('tiny.orm.mysql.model')
        local m = model:new (
            'table_test', -- 表名
            {
                'id',
                'name',
                'short',
                'remark',
                'date'
            },          -- 列的定义
            'tiny',     -- 使用 config/mysql 中的 tiny 配置字段作为连接配置
            'id', --自增 id
        )
  • 从其他的地方加入配置文件

    local model = require('tiny.orm.mysql.model')
    local config = {
        timeout = 3000,
            pool = {
                maxIdleTime = 120000,
                size = 800,
            },
            clusters = {
                master = {"127.0.0.1", "3306"},
                slave = {
                    {"127.0.0.1", "3306"},
                }
            },
            database = "tiny",
            user = "test",
            password = "123456",
            charset = "utf8",
            maxPacketSize = 1024*1024,
    }
    local m = model:new(
        'tiny_user',
        {
            'id',
            'name',
            'passwd'
        },
        config,
        'id'
    )

实现增删改查

        --- 增删改查
        --- 引入 data
        local mysql_fac = require('tiny.orm.mysqlfactory')
        local fac = mysql_fac:new (m)
        --- 新建
        local item = fac:new_item()
        item.name = 'hello world'
        item.show = 'hw'
        fac:create(item)
        --- item.id 已经被赋值
        --- 按 id 查询
        local id = 1
        local item2 = fac:get_by_id(id)
        if (item2~=nil) then
            item2.name = 'new world '
            fac:save(item2) ---- 保存
        end
        --- 删除
        if item2 ~= nil then
            fac:delete(item2)
        end
    

分页

         cal items = nil
         local query = fac:get_query()
         --- select ... from  .. where id = 1
         items = query:where('id',1)
                      :first()
         query = fac:get_query()
         --- select .. from .. where name = 'hello ' limit 10,off 10 ;
         local items2 = query:where('name','hello')
                             :skip(0)
                             :take(10)
                             :get()
         query = fac:get_query()
         --- select  ... from where name = 'hello ' and id in (1,2,3,4)
         local items3 = query:where (name ,'hello')
                             :where_in('id',{1,2,3,4})
                             :get()
         query = fac:get_query()
         --- select .. from .. where .. order by ..
         local items4 = query:order_by('id','asc')
                             :order_by('name')
                             :get()

事务

        local trans = require('tiny.orm.mysql.transaction')
        local t = trans:new()
        t:start()
        --- 各种操作
        t:submit()
        --  提交
        t:rollback()
        -- 回滚

缓存的使用

在 factory 中使用缓存

可以在 factory 实例化时传入第二个参数为缓存配置参数,这时当进行 get_by_id 的操作时时会先从缓存中进行查找,如果没有会查询 mysql 之后同步缓存。

        local model = require('tiny.orm.mysql.model')
        local factory = require('tiny.orm.mysql.factory')
        local cacheFac = require('tiny.cache')
        local user = model:new (
        'auth_user',
        {
            'id',
            'name',
            'passwd',
            'created_at',
            'updated_at',
        },
        'default',
        'id')
        local user_fac = factory:new(user,{cache_type="shared",cache_cfg={catlog="user",expired=10}})
        local user_ins = user_fac: get_by_id ( 3) -- 这里会先在ngx.shared.user中查询是否有相应的缓存
        user_ins.name='namechanged'
        user_fac:save(user_ins) -- 这里会先将数据保存在缓存中。

单独使用缓存

    local cacheFac = require('tiny.cache')
    local str = 'hello word '
    local cache = cacheFac.get('sync')
    
    cache:set (1,str)
    local h = cache:get(1)

缓存的类型及配置

一个 open-tiny-orm 的标准配置如下

        local cache_config = {
                cache_type="redis",
                cache_cfg={
                    expired=1000,
                    key_pre="",
                    redis="default"
                    }
                }

其中: cache_type: 表示缓存类型,目前支持

  1. shared nginx.shared.DICT 缓存

  2. redis redis 共享缓存

  3. sync shared+redis 订阅的同步缓存

cache_cfg: 表示缓存的配置。

  1. cache_cfg.expired 超时的时间,不传的话则默认为 0,即永久存储

  2. cache_cfg.key_pre key 的前缀,不传默认为 ""

  3. cache_cfg.catlog 用于缓存的 ngx.shared.DICT 中的 dict 值,当 cache_type 为 shared 和 sync 时需要设置

  4. cache_cfg.redis 用于缓存的 redis 配置,当 cache_type 为 sync 和 redis 时需要设置

  5. cache_cfg.channel 用于缓存步骤的 redis channel,当 cache_type 为 sync 时需要设置

sync 缓存的同步

sync 缓存是 open-tiny-orm 在 ngx.shared.DICT 基础上加入 redis pubscribe/publish 机制的一种多机同步的缓存,在其正常的使用下,为了保证同步机制的运行,必须要在 init\_周期内加入同步的调用,具体方式如下

init_worker_by_lua_file 内的内容

    local cacheFac = require('tiny.cache') ---缓存fac
    local sync_ins = cacheFac.get('sync') -- 要同步的缓存 
    sync_ins:sync()--开馆始同步

注意 由于 openresty 在 init 周期中对 ngx.var.VARIABLE 的限制,如果人为定义了 open\_tiny\_redis 及 open\_tiny\_cache 的路径,则在这里是不可用的,在这种情况下,请手工引入相应的配置,即传入对应的 table 而不是配置串

作者

yfge(geyunfei@gmail.com)

许可证

3bsd

版本