search

package beegoimport (errorsfmtstrconvsynctime)var (DefaultEvery int = 60 // 1 minute)type BeeItem struct {val interface{}Lastaccess time.Timeexpired int}func (itm *BeeItem) Access() interface{} {itm.Lastaccess = time.Now()return itm.val}ty

package beego

import (
	"errors"
	"fmt"
	"strconv"
	"sync"
	"time"
)

var (
	DefaultEvery int = 60 // 1 minute
)

type BeeItem struct {
	val        interface{}
	Lastaccess time.Time
	expired    int
}

func (itm *BeeItem) Access() interface{} {
	itm.Lastaccess = time.Now()
	return itm.val
}

type BeeCache struct {
	lock  sync.RWMutex
	dur   time.Duration
	items map[string]*BeeItem
	Every int // Run an expiration check Every seconds
}

// NewDefaultCache returns a new FileCache with sane defaults.
func NewBeeCache() *BeeCache {
	cache := BeeCache{dur: time.Since(time.Now()),
		Every: DefaultEvery}
	return &cache
}

func (bc *BeeCache) Get(name string) interface{} {
	bc.lock.RLock()
	defer bc.lock.RUnlock()
	itm, ok := bc.items[name]
	if !ok {
		return nil
	}
	return itm.Access()
}

func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
	bc.lock.Lock()
	defer bc.lock.Unlock()
	t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired}
	if _, ok := bc.items[name]; ok {
		return errors.New("the key is exist")
	} else {
		bc.items[name] = &t
	}
	return nil
}

func (bc *BeeCache) Delete(name string) (ok bool, err error) {
	bc.lock.Lock()
	defer bc.lock.Unlock()
	if _, ok = bc.items[name]; !ok {
		return
	}
	delete(bc.items, name)
	_, valid := bc.items[name]
	if valid {
		ok = false
	}
	return
}

func (bc *BeeCache) IsExist(name string) bool {
	bc.lock.RLock()
	defer bc.lock.RUnlock()
	_, ok := bc.items[name]
	return ok
}

// Start activates the file cache; it will 
func (bc *BeeCache) Start() error {
	dur, err := time.ParseDuration(fmt.Sprintf("%ds", bc.Every))
	if err != nil {
		return err
	}
	bc.dur = dur
	bc.items = make(map[string]*BeeItem, 0)
	go bc.vaccuum()
	return nil
}

func (bc *BeeCache) vaccuum() {
	if bc.Every < 1 {
		return
	}
	for {
		<-time.After(time.Duration(bc.dur))
		if bc.items == nil {
			return
		}
		for name, _ := range bc.items {
			bc.item_expired(name)
		}
	}
}

// item_expired returns true if an item is expired.
func (bc *BeeCache) item_expired(name string) bool {
	bc.lock.Lock()
	defer bc.lock.Unlock()
	itm, ok := bc.items[name]
	if !ok {
		return true
	}
	dur := time.Now().Sub(itm.Lastaccess)
	sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds()))
	if err != nil {
		delete(bc.items, name)
		return true
	} else if sec >= itm.expired {
		delete(bc.items, name)
		return true
	}
	return false
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在Beego中使用Prometheus和Grafana实现监控和报警在Beego中使用Prometheus和Grafana实现监控和报警Jun 22, 2023 am 09:06 AM

随着云计算和微服务的兴起,应用程序的复杂性也随之增加。因此,监控和诊断成为了重要的开发任务之一。在这方面,Prometheus和Grafana是两款颇为流行的开源监控和可视化工具,可以帮助开发者更好地进行应用程序的监测和分析。本文将探讨如何在Beego框架中使用Prometheus和Grafana实现监控和报警。一、介绍Beego是一个开源的快速开发Web应

在Beego中使用Google Analytics统计网站数据在Beego中使用Google Analytics统计网站数据Jun 22, 2023 am 09:19 AM

随着互联网的快速发展,Web应用程序的使用越来越普遍,如何对Web应用程序的使用情况进行监控和分析成为了开发者和网站经营者的关注点。GoogleAnalytics是一种强大的网站分析工具,可以对网站访问者的行为进行跟踪和分析。本文将介绍如何在Beego中使用GoogleAnalytics来统计网站数据。一、注册GoogleAnalytics账号首先需要

Beego中的错误处理——防止应用崩溃Beego中的错误处理——防止应用崩溃Jun 22, 2023 am 11:50 AM

在Beego框架中,错误处理是非常重要的一个部分,因为如果应用程序没有正确、完善的错误处理机制,它可能会导致应用程序崩溃或者无法正常运行,这对我们的项目和用户来说都是一个非常严重的问题。Beego框架提供了一系列的机制来帮助我们避免这些问题,并且使得我们的代码更加健壮、可维护。在本文中,我们将介绍Beego框架中的错误处理机制,并且讨论它们如何帮助我们避免应

Beego中使用JWT实现身份验证Beego中使用JWT实现身份验证Jun 22, 2023 pm 12:44 PM

随着互联网和移动互联网的飞速发展,越来越多的应用需要进行身份验证和权限控制,而JWT(JSONWebToken)作为一种轻量级的身份验证和授权机制,在WEB应用中被广泛应用。Beego是一款基于Go语言的MVC框架,具有高效、简洁、可扩展等优点,本文将介绍如何在Beego中使用JWT实现身份验证。一、JWT简介JSONWebToken(JWT)是一种

在Beego中使用Kong进行API网关管理在Beego中使用Kong进行API网关管理Jun 22, 2023 pm 05:13 PM

随着微服务架构的流行,API网关越来越受到关注。作为微服务架构中的重要组成部分之一,API网关是一个负责分发请求、路由请求以及过滤请求的应用程序。在许多企业中,Kong已成为最流行的API网关之一,因为其灵活、可扩展且易于使用。Beego是一个快速开发Go应用程序的框架,可以提供RESTfulAPI开发的支持。在这篇文章中,我们将探讨如何在Beego中使用

在Beego中使用Hadoop和HBase进行大数据存储和查询在Beego中使用Hadoop和HBase进行大数据存储和查询Jun 22, 2023 am 10:21 AM

随着大数据时代的到来,数据处理和存储变得越来越重要,如何高效地管理和分析大量的数据也成为企业面临的挑战。Hadoop和HBase作为Apache基金会的两个项目,为大数据存储和分析提供了一种解决方案。本文将介绍如何在Beego中使用Hadoop和HBase进行大数据存储和查询。一、Hadoop和HBase简介Hadoop是一个开源的分布式存储和计算系统,它可

展示Web应用数据的新方式——在Beego中使用WebSocket和Socket.io展示Web应用数据的新方式——在Beego中使用WebSocket和Socket.ioJun 22, 2023 am 10:09 AM

随着Web应用程序的发展,我们需要不断探索新的方法来展示数据。其中一个新的方式是使用WebSocket和Socket.io,它们可以实时地更新数据,而不需要重新加载整个页面。本文将介绍如何在Beego中使用WebSocket和Socket.io来展示Web应用程序的数据。Beego是一个基于Go语言的Web框架,它可以帮助我们更容易地构建Web应用程序。首先

如何快速入门Beego开发框架?如何快速入门Beego开发框架?Jun 22, 2023 am 09:15 AM

Beego是一个基于Go语言的开发框架,它提供了一套完整的Web开发工具链,包括路由、模板引擎、ORM等。如果你想快速入门Beego开发框架,以下是一些简单易懂的步骤和建议。第一步:安装Beego和Bee工具安装Beego和Bee工具是开始学习Beego的第一步。你可以在Beego官网上找到详细的安装步骤,也可以使用以下命令来安装:gogetgithub

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment