XU XUv0.16.0

#github.com/xulang-org/packages/memcached

导入方式: memcached #github.com/xulang-org/packages/memcached

公开 API: 5 函数, 28 方法, 3 类型, 0 常量, 0 方法集

扩展包 `memcached #github.com/xulang-org/packages/memcached`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

23_standalone_memcached/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

memcached #github.com/xulang-org/packages/memcached
#os

fn main() int {
    println("=== Memcached Standalone Library Integration Test ===")
    addr := "127.0.0.1:11211"
    println("Connecting to Memcached at " + addr + "...")

    $mc := memcached.open(addr + "") >>> (err) {
        println("Connection to Memcached failed (skipping live tests): " + err.msg())
        << 0
    }

    ver := mc.version() >>> (err) {
        println("Version failed: " + err.msg())
        << 1
    }
    println("1. Memcached version: " + ver)

    // Set & Get
    set_ok := mc.set("xu_test_key", "Hello Memcached from Xu!", 0, 300) >>> (err) {
        println("Set failed: " + err.msg())
        << 1
    }
    println("2. SET xu_test_key ok=" + str(set_ok))

    val := mc.get("xu_test_key") >>> (err) {
        println("Get failed: " + err.msg())
        << 1
    }
    println("3. GET xu_test_key: " + val)

    // Incr & Decr
    mc.set("xu_counter", "100", 0, 300) >>> (err) {
        << 1
    }
    new_n := mc.incr("xu_counter", 5) >>> (err) {
        println("Incr failed: " + err.msg())
        << 1
    }
    println("4. INCR xu_counter by 5 = " + str(new_n))

    decr_n := mc.decr("xu_counter", 2) >>> (err) {
        println("Decr failed: " + err.msg())
        << 1
    }
    println("5. DECR xu_counter by 2 = " + str(decr_n))

    // Append & Prepend
    mc.set("xu_str", "World", 0, 300) >>> (err) {
        << 1
    }
    mc.prepend("xu_str", "Hello ") >>> (err) {
        << 1
    }
    mc.append("xu_str", "!") >>> (err) {
        << 1
    }
    prep_val := mc.get("xu_str") >>> (err) {
        << 1
    }
    println("6. Prepend & Append result: " + prep_val)

    // Delete
    del_ok := mc.delete("xu_test_key") >>> (err) {
        << 1
    }
    println("7. DELETE xu_test_key ok=" + str(del_ok))

    mc.close()

    // Test Pool with Reuse & Concurrency
    pool := memcached.new_pool(addr + "", 5)
    $p_conn := pool.get() >>> (err) {
        println("Pool get failed: " + err.msg())
        << 1
    }
    fd_first := p_conn.fd
    p_ver := p_conn.version() >>> (err) {
        << 1
    }
    println("8. Pool connection version: " + p_ver + " (fd=" + str(fd_first) + ")")
    pool.put(p_conn)
    println("9. Pool connection returned successfully.")

    // Verify Pool Connection REUSE: second get gives same fd
    $p_conn2 := pool.get() >>> (err) {
        println("Pool second get failed: " + err.msg())
        << 1
    }
    fd_second := p_conn2.fd
    if fd_first == fd_second {
        println("10. [PASS] Pool Connection REUSE verified! (fd_first == fd_second: " + str(fd_first) + ")")
    } else {
        println("10. [WARN] Pool connection not reused: fd1=" + str(fd_first) + ", fd2=" + str(fd_second))
    }

    // Concurrent borrow another connection
    $p_conn3 := pool.get() >>> (err) {
        println("Pool third get failed: " + err.msg())
        << 1
    }
    fd_third := p_conn3.fd
    if fd_second != fd_third {
        println("11. [PASS] Pool Multi-connection concurrency verified! (fd2=" + str(fd_second) + " != fd3=" + str(fd_third) + ")")
    }
    p_conn3.set("pool_mc_key", "mc_val", 0, 300) >>> (err) {
        << 1
    }
    got_mc := p_conn2.get("pool_mc_key") >>> (err) {
        << 1
    }
    println("12. [PASS] Concurrent connection cross-get verified: got=" + got_mc)

    pool.put(p_conn2)
    pool.put(p_conn3)
    pool.close()
    println("13. [PASS] Memcached Pool closed cleanly.")

    println("=== Memcached Tests Completed Successfully! ===")
    << 0
}

API 声明

open

XuAPI
pub fn open(addr string) Client

用途

打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。

参数

addr string
地址(string)

返回值

Client — 该类型的值

查看用法示例

struct Client

XuAPI
pub struct Client {
    pub fd int
    pub buf string
    pub addr string
}

用途

Memcached 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

fd int
文件描述符
buf string
缓冲
addr string
地址

dial

XuAPI
pub fn dial(addr string) Client

用途

发起连接。失败时抛出错误,调用方需处理或继续传播。

参数

addr string
地址(string)

返回值

Client — 该类型的值

($&Client) read_line

XuAPI
pub fn (c $&Client) read_line() string

用途

$&Client 上的操作:执行 read line 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

string — 结果字符串

($&Client) read_bytes

XuAPI
pub fn (c $&Client) read_bytes(n int) string

用途

$&Client 上的操作:读到 delim(含 delim)。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

string — 结果字符串

($&Client) write_raw

XuAPI
pub fn (c $&Client) write_raw(data string) int

用途

$&Client 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&Client) close

XuAPI
pub fn (c $&Client) close() int

用途

$&Client 上的操作:关闭资源。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&Client) set

XuAPI
pub fn (c $&Client) set(key string, val string, flags int, exptime int) bool

用途

在 $&Client 上执行 set 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)
flags int
flags(int)
exptime int
exptime(int)

返回值

bool — 是否成立

($&Client) add

XuAPI
pub fn (c $&Client) add(key string, val string, flags int, exptime int) bool

用途

$&Client 上的操作:相加。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)
flags int
flags(int)
exptime int
exptime(int)

返回值

bool — 是否成立

($&Client) replace

XuAPI
pub fn (c $&Client) replace(key string, val string, flags int, exptime int) bool

用途

$&Client 上的操作:替换子串,次数由 n 限制。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)
flags int
flags(int)
exptime int
exptime(int)

返回值

bool — 是否成立

($&Client) append

XuAPI
pub fn (c $&Client) append(key string, val string) bool

用途

$&Client 上的操作:追加。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

bool — 是否成立

($&Client) prepend

XuAPI
pub fn (c $&Client) prepend(key string, val string) bool

用途

在 $&Client 上执行 prepend 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

bool — 是否成立

($&Client) get

XuAPI
pub fn (c $&Client) get(key string) string

用途

在 $&Client 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)

返回值

string — 结果字符串

($&Client) get_multi

XuAPI
pub fn (c $&Client) get_multi(keys []string) []string

用途

$&Client 上的操作:读取 multi。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
keys []string
keys([]string)

返回值

[]string — 切片结果

($&Client) delete

XuAPI
pub fn (c $&Client) delete(key string) bool

用途

$&Client 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)

返回值

bool — 是否成立

($&Client) incr

XuAPI
pub fn (c $&Client) incr(key string, delta int) int

用途

在 $&Client 上执行 incr 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
delta int
增量(int)

返回值

int — 该类型的值

($&Client) decr

XuAPI
pub fn (c $&Client) decr(key string, delta int) int

用途

在 $&Client 上执行 decr 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
delta int
增量(int)

返回值

int — 该类型的值

($&Client) touch

XuAPI
pub fn (c $&Client) touch(key string, exptime int) bool

用途

在 $&Client 上执行 touch 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
exptime int
exptime(int)

返回值

bool — 是否成立

($&Client) version

XuAPI
pub fn (c $&Client) version() string

用途

在 $&Client 上执行 version 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

string — 结果字符串

($&Client) flush_all

XuAPI
pub fn (c $&Client) flush_all() bool

用途

$&Client 上的操作:执行 flush all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

bool — 是否成立

($&Client) stats

XuAPI
pub fn (c $&Client) stats() []string

用途

在 $&Client 上执行 stats 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

[]string — 切片结果

struct Pool

XuAPI
pub struct Pool {
    pub addr string
    pub idle chan int
    pub max_idle int
}

用途

Memcached 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

addr string
地址
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(addr string, max_idle int) Pool

用途

构造 pool。

参数

addr string
地址(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) put

XuAPI
pub fn (p &Pool) put(c Client)

用途

写入或更新指定键对应的值数据。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
c Client
字节或字符码(Client)

返回值

不返回值。

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

用途

Memcached 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)

返回值

TlsClient — 该类型的值

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)
cfg tls.Config
cfg(tls.Config)

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

用途

$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

用途

$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int
need(int)

返回值

string — 结果字符串

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值