[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-caching-go-speed-up-program-all--*":3,"academy-blog-translations-5wdhll2r8vt45vr":129},{"data":4,"page":117,"perPage":117,"totalItems":117,"totalPages":117},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":123,"keywords":124,"locale":99,"published_at":125,"scheduled_at":13,"school_blog":121,"short_description":126,"slug":127,"status":119,"title":6,"updated":128,"updated_by":13,"views":122},"Ep.20 Go and Caching Enhancing Program Speed!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cstrong>Go and Caching: Enhancing Program Speed\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>What is Caching?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Caching is the process of storing certain data in memory so that it can be accessed quickly without having to retrieve it from the original source repeatedly. For example, storing frequently accessed results from a database in memory allows a program to load data faster.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Benefits of Caching\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>1. Increased Speed: Reduces the need to fetch data from external sources (like databases), enhancing data access speed.\u003C\u002Fp>\u003Cp>2. Resource Efficiency: Minimizes redundant data retrieval, thereby reducing system workload.\u003C\u002Fp>\u003Cp>3. Ideal for Static Data: Particularly suitable for data that does not change frequently, such as product listings, default values, etc.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Creating a Simple Cache Using Map in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>In Go, we can create a simple cache using a map, which serves to store data in memory and allows for quick data retrieval.\u003C\u002Fp>\u003Cp>Example of a Simple Cache:\u003C\u002Fp>\u003Cp>In this example, we define a Cache struct that contains a map for storing data. The Set method is used to store data in the cache, while the Get method retrieves data from the cache.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\n\nimport (\n    \"fmt\"\n)\n\ntype Cache struct {\n    data map[string]string\n}\n\nfunc (c *Cache) Set(key, value string) {\n    c.data[key] = value\n}\n\nfunc (c *Cache) Get(key string) (string, bool) {\n    value, found := c.data[key]\n    return value, found\n}\n\nfunc main() {\n    cache := Cache{data: make(map[string]string)}\n    cache.Set(\"name\", \"Alice\")\n    cache.Set(\"country\", \"Thailand\")\n\n    if value, found := cache.Get(\"name\"); found {\n        fmt.Println(\"Cached value:\", value)\n    } else {\n        fmt.Println(\"Value not found in cache\")\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Setting Expiration Time for Cache (TTL - Time to Live)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Sometimes, we want cached data to expire after a certain period to ensure that the information being used is up-to-date.\u003C\u002Fp>\u003Cp>Example Code for Setting TTL in Cache:\u003C\u002Fp>\u003Cp>In this example:\u003C\u002Fp>\u003Cp>We add an expiration time to the cached data using time.Now().Add(ttl).Unix() to define the expiry time.\u003C\u002Fp>\u003Cp>When checking the data in the cache, we verify whether the expiration time has passed. If it has expired, we consider that the data is no longer available in the cache.\u003C\u002Fp>\u003Cp>This approach helps maintain the freshness of the data while still benefiting from the speed of caching.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\ntype CacheItem struct {\n    value      string\n    expiration int64\n}\n\ntype Cache struct {\n    data map[string]CacheItem\n}\n\nfunc (c *Cache) Set(key, value string, ttl time.Duration) {\n    expiration := time.Now().Add(ttl).Unix()\n    c.data[key] = CacheItem{value: value, expiration: expiration}\n}\n\nfunc (c *Cache) Get(key string) (string, bool) {\n    item, found := c.data[key]\n    if !found || time.Now().Unix() &gt; item.expiration {\n        return \"\", false\n    }\n    return item.value, true\n}\n\nfunc main() {\n    cache := Cache{data: make(map[string]CacheItem)}\n    cache.Set(\"session_id\", \"abc123\", 5*time.Second)\n\n    if value, found := cache.Get(\"session_id\"); found {\n        fmt.Println(\"Cached value:\", value)\n    } else {\n        fmt.Println(\"Value not found in cache or expired\")\n    }\n\n    time.Sleep(6 * time.Second) \u002F\u002F รอให้ Cache หมดอายุ\n\n    if value, found := cache.Get(\"session_id\"); found {\n        fmt.Println(\"Cached value:\", value)\n    } else {\n        fmt.Println(\"Value not found in cache or expired\")\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Example of Using Cache in Real Applications\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>In real applications, caching is often used for data that does not change frequently, such as user information, products, or settings. This allows programs to retrieve data faster and reduces redundant loads from the database.\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>In Summary:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>Cache Increases Speed: It stores frequently accessed data to minimize repeated loading.\u003C\u002Fli>\u003Cli>Set TTL: Configure the cache to expire after a specified time to mitigate issues with outdated information.\u003C\u002Fli>\u003Cli>Use Map in Go for Cache: Maps enable quick data retrieval, making them ideal for caching purposes.\u003C\u002Fli>\u003C\u002Ful>","10_11zon_cyiuijwhvy.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fbgnmn6rgfwua032\u002F10_11zon_cyiuijwhvy.webp","2026-03-04 08:51:32.343Z","",{"keywords":15,"locale":93,"school_blog":103},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:57.314Z","dsuxbh25sae33r6","programmer","2026-04-10 16:07:48.083Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:32:28.182Z","q6jkfvmwi47km1s","programmer learning","2026-04-10 16:07:54.681Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:32:11.227Z","z68fk5epz69djvb","programmer education","2026-04-10 16:07:51.001Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:31:48.594Z","4lt76y5iyl22l25","learning programming","2026-04-10 16:07:45.383Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:32:17.000Z","ijeopq81rmv6mt7","Programming Class","2026-04-10 16:07:52.028Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:32:51.346Z","tmzmy6jyz1n35rr","Go Programming","2026-04-10 16:08:01.434Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":94,"collectionId":95,"collectionName":96,"created":97,"flag":98,"id":99,"is_default":100,"label":101,"updated":102},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":104,"collectionId":105,"collectionName":106,"expand":107,"id":121,"views":122},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":108},{"blogIds":109,"collectionId":110,"collectionName":111,"created":112,"created_by":13,"id":104,"image":113,"image_alt":13,"image_path":114,"label":115,"name":116,"priority":117,"publish_at":118,"scheduled_at":13,"status":119,"updated":120,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":116,"th":116},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","5wdhll2r8vt45vr",208,"bgnmn6rgfwua032",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90],"2025-01-27 04:49:19.650Z","Learn about caching in Go to increase program speed by storing data in memory and how to set expiration time (TTL) for cached data.","caching-go-speed-up-program","2026-04-22 07:11:48.007Z",{"en":127}]