先看一下我们讲到哪里了:

cache2go的源码前面我们已经讲完了cacheitem和cachetable的实现,今天cahce和examples会一起讲完~

1、cache.go源码

​        前面的代码看完之后现在看cache.go就太简单了,上代码吧~

 1var (
2    cache = make(map[string]*CacheTable)
3    mutex sync.RWMutex
4)
5
6// Cache returns the existing cache table with given name or creates a new one
7// if the table does not exist yet.
8//【表存在则返回表,表不存在的时候创建一个不包含items的空表,然后返回之】
9func Cache(table string) *CacheTable {
10    mutex.RLock()
11    //【注意cache的类型,这是一个用于存CacheTable的map】
12    t, ok := cache[table]
13    mutex.RUnlock()
14
15    if !ok {
16        mutex.Lock()
17        //【表不存在的时候需要创建一个空表,这时候做了一个二次检查,为的是并发安全】
18        t, ok = cache[table]
19        // Double check whether the table exists or not.
20        if !ok {
21            t = &CacheTable{
22                name:  table,
23                items: make(map[interface{}]*CacheItem),
24            }
25            cache[table] = t
26        }
27        mutex.Unlock()
28    }
29
30    return t
31}

2、example

  如上图,项目中还有一个examples目录,大家肯定已经猜到了里面的内容了,没错,就是上面介绍的一堆代码组成的缓存库,怎么玩?

  下面我们一个一个来看这3个示例吧~

1、callbacks.go

 1func main() {
2    //【创建一个名为myCache的缓存表】
3    cache := cache2go.Cache("myCache")
4
5    // This callback will be triggered every time a new item
6    // gets added to the cache.
7    //【每次有新item被加入到这个缓存表的时候会被触发的回调函数】
8    //【这个函数只做了一个输出的动作】
9    cache.SetAddedItemCallback(func(entry *cache2go.CacheItem) {
10        fmt.Println("Added:", entry.Key(), entry.Data(), entry.CreatedOn())
11    })
12    // This callback will be triggered every time an item
13    // is about to be removed from the cache.
14    //【当一个item被删除时被触发执行的回调函数,同样只有一个打印功能】
15    cache.SetAboutToDeleteItemCallback(func(entry *cache2go.CacheItem) {
16        fmt.Println("Deleting:", entry.Key(), entry.Data(), entry.CreatedOn())
17    })
18
19    // Caching a new item will execute the AddedItem callback.
20    //【缓存中添加一条记录】
21    cache.Add("someKey", 0, "This is a test!")
22
23    // Let's retrieve the item from the cache
24    //【读取刚才存入的数据】
25    res, err := cache.Value("someKey")
26    if err == nil {
27        fmt.Println("Found value in cache:", res.Data())
28    } else {
29        fmt.Println("Error retrieving value from cache:", err)
30    }
31
32    // Deleting the item will execute the AboutToDeleteItem callback.
33    //【删除someKey对应的记录】
34    cache.Delete("someKey")
35
36    // Caching a new item that expires in 3 seconds
37    //【添加设置了3s存活时间的记录】
38    res = cache.Add("anotherKey", 3*time.Second, "This is another test")
39
40    // This callback will be triggered when the item is about to expire
41    //【一旦触发了删除操作就会调用到下面这个回调函数,在这里也就是3s到期时被执行
42    res.SetAboutToExpireCallback(func(key interface{}) {
43        fmt.Println("About to expire:", key.(string))
44    })
45
46    //【为了等上面的3s时间到】
47    time.Sleep(5 * time.Second)
48}

2、dataloader.go

 1func main() {
2    cache := cache2go.Cache("myCache")
3
4    // The data loader gets called automatically whenever something
5    // tries to retrieve a non-existing key from the cache.
6    //【当从cache中访问一个不存在的key时会触发这个回调函数】
7    cache.SetDataLoader(func(key interface{}, args ...interface{}) *cache2go.CacheItem {
8        // Apply some clever loading logic here, e.g. read values for
9        // this key from database, network or file.
10        //【这里可以做一些机智的处理,比如说从数据库,网络或者文件中读取数据,这当然也是缓存的意义】
11        //key.(string)是类型断言,将interface{}类型的数据转回到string类型
12        val := "This is a test with key " + key.(string)
13
14        // This helper method creates the cached item for us. Yay!
15        //【很棒,这样就构造了一个value,然后构造出item】
16        item := cache2go.NewCacheItem(key, 0, val)
17        return item
18    })
19
20    // Let's retrieve a few auto-generated items from the cache.
21    //【试着检索一些自动生成的items】
22    for i := 0; i < 10; i++ {
23        //【将i转换为字符串,拼接成someKey_1的形式】
24        res, err := cache.Value("someKey_" + strconv.Itoa(i))
25        if err == nil {
26            fmt.Println("Found value in cache:", res.Data())
27        } else {
28            fmt.Println("Error retrieving value from cache:", err)
29        }
30    }
31}

3、mycachedapp.go

 1// Keys & values in cache2go can be of arbitrary types, e.g. a struct.
2//【这个例子中要存储的数据是如下结构体类型】
3type myStruct struct {
4    text     string
5    moreData []byte
6}
7
8func main() {
9    // Accessing a new cache table for the first time will create it.
10    //【创建缓存表myCache】
11    cache := cache2go.Cache("myCache")
12
13    // We will put a new item in the cache. It will expire after
14    // not being accessed via Value(key) for more than 5 seconds.
15    //【构造一个数据】
16    val := myStruct{"This is a test!", []byte{}}
17    //【存入数据,设置存活时间为5s】
18    cache.Add("someKey", 5*time.Second, &val)
19
20    // Let's retrieve the item from the cache.
21    //【试着读取】
22    res, err := cache.Value("someKey")
23    if err == nil {
24        fmt.Println("Found value in cache:", res.Data().(*myStruct).text)
25    } else {
26        fmt.Println("Error retrieving value from cache:", err)
27    }
28
29    // Wait for the item to expire in cache.
30    //【等待6s之后,明显是该过期了】
31    time.Sleep(6 * time.Second)
32    res, err = cache.Value("someKey")
33    if err != nil {
34        fmt.Println("Item is not cached (anymore).")
35    }
36
37    // Add another item that never expires.
38    //【再存入一个永不过期的数据】
39    cache.Add("someKey", 0, &val)
40
41    // cache2go supports a few handy callbacks and loading mechanisms.
42    //【设置回调函数,删除数据的时候被调用】
43    cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {
44        fmt.Println("Deleting:", e.Key(), e.Data().(*myStruct).text, e.CreatedOn())
45    })
46
47    // Remove the item from the cache.
48    //【手动执行删除操作,当然,上面设置的函数将被调用】
49    cache.Delete("someKey")
50
51    // And wipe the entire cache table.
52    //【抹去整个cache中的数据】
53    cache.Flush()
54}

  终于全部讲完了~

  如上,整个项目除了测试代码外,实现逻辑部分全部分析完了,cache2go算是非常简短的go开源项目了,很适合作为第一个读源码项目,下面要分析的groupcache也是和cache相关,但是相比cache2go要复杂很多,groupcache是大名鼎鼎的memcache的go语言版,我们下周开始分析!

cache2go源码最后一讲 - examples的更多相关文章

  1. Pytorch学习之源码理解:pytorch/examples/mnists

    Pytorch学习之源码理解:pytorch/examples/mnists from __future__ import print_function import argparse import ...

  2. 启航 - cache2go源码分析

    一.概述 我们今天开始第一部分“golang技能提升”.这一块我计划分析3个项目,一个是很流行的golang源码阅读入门项目cache2go,接着是非常流行的memcache的go语言版groupca ...

  3. Kettle 4.2源码分析第二讲--Kettle插件结构体系简介

    1.  插件体系结构 1.1. 插件技术原理 1.1.1.    插件概念说明 插件是一种遵循统一的预定义接口规范编写出来的程序,应用程序在运行时通过接口规范对插件进行调用,以扩展应用程序的功能.在英 ...

  4. 建议收藏!利用Spring解决循环依赖,深入源码给你讲明白!

    前置知识 只有单例模式下的bean会通过三级缓存提前暴露来解决循环依赖的问题.而非单例的bean每次获取都会重新创建,并不会放入三级缓存,所以多实例的bean循环依赖问题不能解决. 首先需要明白处于各 ...

  5. Kettle 4.2源码分析第一讲--Kettle 简介

    Pentaho Data Integration(PDI)简介 1. PDI结构简介 图 1‑1 PDI核心组件 Spoon是构建ETL Jobs和Transformations的工具.Spoon可以 ...

  6. anki_vector SDK源码解析(教程)

    一:最近anki vector robot开放了Python SDK,我听到的第一时间就赶快上网查了查,先抛几个官网重要链接吧: Python编程API手册及环境搭建等: https://sdk-re ...

  7. Redux异步解决方案之Redux-Thunk原理及源码解析

    前段时间,我们写了一篇Redux源码分析的文章,也分析了跟React连接的库React-Redux的源码实现.但是在Redux的生态中还有一个很重要的部分没有涉及到,那就是Redux的异步解决方案.本 ...

  8. 手写@koa/router源码

    上一篇文章我们讲了Koa的基本架构,可以看到Koa的基本架构只有中间件内核,并没有其他功能,路由功能也没有.要实现路由功能我们必须引入第三方中间件,本文要讲的路由中间件是@koa/router,这个中 ...

  9. 可视化工具gephi源码探秘(二)---导入netbeans

    在上篇<可视化工具gephi源码探秘(一)>中主要介绍了如何将gephi的源码导入myeclipse中遇到的一些问题,此篇接着上篇而来,主要讲解当下通过myeclipse导入gephi源码 ...

随机推荐

  1. pyenv global 设置失败 pyenv local 设置就成功了 不知道啥原因

    dev@PC-20190309QPVT:/mnt/c/data/htdocs/python/flaskr$ pyenv global 3.6.1dev@PC-20190309QPVT:/mnt/c/d ...

  2. js函数柯里化,实现bind

    1.柯里化: 把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术. 举个栗子: 一个计算两数之和的函数,需要传递两个参数,柯里化 ...

  3. laravel5单元测试

    https://www.cnblogs.com/love-snow/articles/7641198.html

  4. MS SQL计算最大公约数和最小公倍数函数

    /*求两个数的最大公约数*/ CREATE FUNCTION f_GetGys ( @num1 BIGINT , @num2 BIGINT ) RETURNS BIGINT AS BEGIN DECL ...

  5. svn 安装及更新web库

    安装: apt-get update         //更新apt-get库 apt-get install subversion        安装SVN mkdir /home/svn     ...

  6. es5中的for in 与es6中的for of的用法与区别

    for in 用与循环遍历对象中的属性键值 for of用于循环遍历出数组中的属性值 for in 也可以遍历数组,但是局限是他会把数组的其他属性键值也会遍历出,例如给数组添加一个属性arr.name ...

  7. 一个自己研究出来的字符串匹配算法-k子串算法

    前言 最近工作中需要写一个算法,而写完这个算法我却发现了一个很有意思的事情.需要的这个算法是这样的:对于A,B两个字符串,找出最多K个公共子串,使得这K个子串长度和最大.百度之没有这样的算法,然后就开 ...

  8. Hadoop 数据去重

    数据去重这个实例主要是为了读者掌握并利用并行化思想对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问等这些看似庞杂的任务都会涉及数据去重.下面就进入这个实例的MapReduce ...

  9. Android 实现卡片翻转的动画(翻牌动画)

    Android 实现卡片翻转的动画(翻牌动画) 需求描述 点击卡片,卡片翻转过来显示内容. 点击左边的卡片,将卡片翻转显示右边的图片结果. 功能实现 因为要翻转所以使用动画来完成翻转的动画.动画分为两 ...

  10. 微信小程序快速开发上手

    微信小程序快速开发上手 介绍: 从实战开发角度,完整系统地介绍了小程序的开发环境.小程序的结构.小程序的组件与小程序的API,并提供了多个开发实例帮助读者快速掌握小程序的开发技能,并能自己动手开发出小 ...