{"id":13411760,"url":"https://github.com/oaStuff/clusteredBigCache","last_synced_at":"2025-03-14T17:31:06.358Z","repository":{"id":57487112,"uuid":"114611101","full_name":"oaStuff/clusteredBigCache","owner":"oaStuff","description":"golang bigcache with clustering as a library.","archived":false,"fork":false,"pushed_at":"2018-01-22T22:02:54.000Z","size":134,"stargazers_count":44,"open_issues_count":2,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-07-31T20:48:46.002Z","etag":null,"topics":["caching","clustering","go","golang","networking"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oaStuff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-18T07:48:07.000Z","updated_at":"2023-11-01T01:52:19.000Z","dependencies_parsed_at":"2022-09-01T23:01:40.256Z","dependency_job_id":null,"html_url":"https://github.com/oaStuff/clusteredBigCache","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oaStuff%2FclusteredBigCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oaStuff%2FclusteredBigCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oaStuff%2FclusteredBigCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oaStuff%2FclusteredBigCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oaStuff","download_url":"https://codeload.github.com/oaStuff/clusteredBigCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618659,"owners_count":20320273,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","clustering","go","golang","networking"],"created_at":"2024-07-30T20:01:16.625Z","updated_at":"2025-03-14T17:31:05.842Z","avatar_url":"https://github.com/oaStuff.png","language":"Go","readme":"clusteredBigCache\n=================\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/oastuff/clusteredBigCache)](https://goreportcard.com/report/github.com/oastuff/clusteredBigCache)\n\nThis is a library based on [bigcache](https://github.com/allegro/bigcache) with some modifications to support\n* clustering and\n* individual item expiration\n\nBigcache is an excellent piece of software but the fact that items could only expire based on a predefined \nvalue was not just too appealing. Bigcache had to be modified to support individual expiration of items using\na single timer. This happens by you specifying a time value as you add items to the cache.\nRunning two or more instances of an application that would require some level of caching would normally\ndefault to memcache or redis which are external applications adding to the mix of services required for your\napplication to run.\n\nWith clusteredBigCache there is no requirement to run an external application to provide caching for multiple\ninstances of your application. The library handles caching as well as clustering the caches between multiple\ninstances of your application providing you with simple library APIs (by just calling functions) to store and\nget your values.\n\nWith clusteredBigCache, when you store a value in one instance of your application and every other instance \nor any other application for that matter that you configure to form/join your \"cluster\" will\nsee that exact same value.\n\n## Installing\n\n### Using *go get*\n\n    $ go get github.com/oaStuff/clusteredBigCache\n\n## Samples 1\n\nThis is the application responsible for storing data into the cache\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"bufio\"\n    \"os\"\n    \"github.com/oaStuff/clusteredBigCache/Cluster\"\n    \"strings\"\n    \"time\"\n)\n\n//\n//main function\n//\nfunc main() {\n    fmt.Println(\"starting...\")\n    cache := clusteredBigCache.New(clusteredBigCache.DefaultClusterConfig(), nil)\n    count := 1\n    cache.Start()\n\n    reader := bufio.NewReader(os.Stdin)\n    var data string\n    for strings.ToLower(data) != \"exit\" {\n        fmt.Print(\"enter data : \")\n        data, _ = reader.ReadString('\\n')\n        data = strings.TrimSpace(data)\n        err := cache.Put(fmt.Sprintf(\"key_%d\", count), []byte(data), time.Minute * 60)\n        if err != nil {\n            panic(err)\n       \t}\n       \tfmt.Printf(\"'%s' stored under key 'key_%d'\\n\", data, count)\n       \tcount++\n   \t}\n}\n\n```\n\n##### Explanation:\n\nThe above application captures data from the keyboard and stores them inside clusteredBigCache\nstarting with keys 'key_1', 'key_2'...'key_n'. As the user types and presses the enter key the data is stored in\nthe cache.\n\n`cache := clusteredBigCache.New(clusteredBigCache.DefaultClusterConfig(), nil)`\nThis statement will create the cache using the default configuration. This configuration has default \nvalues for *LocalPort = 9911*, *Join = false* amongst others. If you intend to use this library for applications\nthat will run on the same machine, you will have to give unique values for *LocalPort*\n\n`cache.Start()` This **must** be called before using any other method on this cache.\n\n`err := cache.Put(fmt.Sprintf(\"key_%d\", count), []byte(data), time.Minute * 60)`. You set values in the cache\ngiving it a key, the data as a `[]byte` slice and the expiration or time to live (ttl) for that key/value within the cache.\nWhen the key/value pair reaches its expiration time, they are removed automatically.\n\n\n## Samples 2\n\nThis is the application responsible for reading data of the cache. This can be run on the same or different machine\non the network.\n\n```go\npackage main\n\nimport (\n    \"github.com/oaStuff/clusteredBigCache/Cluster\"\n    \"bufio\"\n    \"os\"\n    \"strings\"\n    \"fmt\"\n    \"time\"\n)\n\n//\n//\nfunc main() {\n    config := clusteredBigCache.DefaultClusterConfig()\n    config.LocalPort = 8888\n    config.Join = true\n    config.JoinIp = \"127.0.0.1:9911\"\n    cache := clusteredBigCache.New(config, nil)\n    err := cache.Start()\n    if err != nil {\n        panic(err)\n    }\n    \n    reader := bufio.NewReader(os.Stdin)\n    var data string\n    for strings.ToLower(data) != \"exit\" {\n        fmt.Print(\"enter key : \")\n        data, _ = reader.ReadString('\\n')\n        data = strings.TrimSpace(data)\n        value, err := cache.Get(data, time.Millisecond * 160)\n        if err != nil {\n            fmt.Println(err)\n            continue\n        }\n        fmt.Printf(\"you got '%s' from the cache\\n\", value)\n    }\n}\n\n```\n\n##### Explanation:\n\nThe above application reads a string from the keyboard which should represent a key for a value in clusteredBigCache.\nIf a user enters the corresponding keys shown in sample1 above ('key_1', 'key_2'...'key_n'), the corresponding values\nwill be returned.\n\n```go\n    config := clusteredBigCache.DefaultClusterConfig()\n    config.LocalPort = 8888\n    config.Join = true\n    config.JoinIp = \"127.0.0.1:9911\"\n    cache := clusteredBigCache.New(config, nil)\n    err := cache.Start()\n```\n\nThe above uses the default configuration to create a config and modifies what it actually needs.\n`config.LocalPort = 8888` has to be changed since this application will run on the same machine with the sample1 \napplication. This is to avoid 'port already in use' errors. \n\n`config.Join = true`. For an application to join another\napplication or applications using clusteredBigCache, it **must** set *config.Join* value to *true* and set `config.JoinIP` to \nthe IP address of one of the systems using clusteredBigCache eg `config.Join = \"127.0.0.1:9911`. This example says that this application \nwants to join another application using clusteredBigCache at IP address *127.0.0.1* and port number *9911*.\n\n`cache := clusteredBigCache.New(config, nil)` creates the cache and `cache.Start()` must be called to start everything running.\n\n__**NB**__\n\nAfter `cache.Start()` is called the library tries to connect to the specified IP address using the specified port. \nWhen successfully connected, it create a cluster of applications using clusteredBigCache as a single cache. ie all applications connected will see every value \nevery application sets in the cache.\n\n#### Sample way to parse config in an app\n\n```go\n    join := flag.String(\"join\", \"\", \"ipAddr:port number of remote server\")\n    localPort := flag.Int(\"port\", 6060, \"local server port to bind to\")\n\n    \n    flag.Parse()\n    \n    config := clusteredBigCache.DefaultClusterConfig()\n    if *join != \"\" {\n        config.JoinIp = *join\n        config.Join = true\n    }\n    config.LocalPort = *localPort\n```\n\nYour application could pass parameters to it in any form and make use of them in configuring clusteredBigCache. The\nabove sample just only catered for `join` and `localport`. If you want network connections between machine to be reconnected\nin the event of a disconnection, you will have to set `config.ReconnectOnDisconnect = true`.\n\n#### Logging within the library\n\nclusteredBigCache takes a second parameter is its New() function for logging.\nThis function expects an interface of \n```go\ntype AppLogger interface {\n    Info(msg string)\n    Warn(msg string)\n    Critical(msg string)\n    Error(msg string)\n}\n```\n\nYou could easily just wrap any logger within a `struct` and provide this interface method for that struct and simple\ndelegate calls to the underlining logger or better still just wrap a logger function to provide the interface like example bellow\n\n```go\ntype myLogger func(...interface{})\n\nfunc (log myLogger) Info(msg string)  {\n\tlog(msg)\n}\n\nfunc (log myLogger) Warn(msg string)  {\n\tlog(msg)\n}\n\nfunc (log myLogger) Error(msg string)  {\n\tlog(msg)\n}\n\nfunc (log myLogger) Critical(msg string)  {\n\tlog(msg)\n}\n\n\ncache := clusteredBigCache.New(config, myLogger(log.Println))\n\n```\n\n### Using Passive client\nPassive client are nodes in the clusteredBigCache network that do not store any data locally but functions all the same \nlike every other node. To create a passive client you simply call `clusteredBigCache.NewPassiveClient(\"linux_box_100\",\"localhost:9090\", 8885, 0, 0, 0, nil)`\nThis will connect to an existing cluster at address *localhost:9090* and join the cluster. the *linux_box_100* is the node's id.\nThis can be an empty string if you want an auto generated id. Every other function can be performed\non the returned object.  \n\n##### credits\nCore cache system from [bigcache](https://github.com/allegro/bigcache)\n\nData structures from [emirpasic](https://github.com/emirpasic/gods)\n\n### LICENSE\nMIT.\n\n\n\n\n","funding_links":[],"categories":["Uncategorized","Database","数据库","Generators","数据库  `go语言实现的数据库`","Data Integration Frameworks"],"sub_categories":["Caches","缓存","Advanced Console UIs","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FoaStuff%2FclusteredBigCache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FoaStuff%2FclusteredBigCache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FoaStuff%2FclusteredBigCache/lists"}