[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-caching-go-speed-up-program-all--*":3,"academy-blog-translations-u6mc5p13ui5399n":144},{"data":4,"page":132,"perPage":132,"totalItems":132,"totalPages":132},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":138,"keywords":139,"locale":114,"published_at":140,"scheduled_at":13,"school_blog":136,"short_description":141,"slug":142,"status":134,"title":6,"updated":143,"updated_by":13,"views":137},"Ep.20 Go กับ Caching เพิ่มความเร็วให้โปรแกรม","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go กับ Caching เพิ่มความเร็วให้โปรแกรม\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Caching คืออะไร?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Caching คือการเก็บข้อมูลบางอย่างไว้ในหน่วยความจำ เพื่อให้สามารถเข้าถึงข้อมูลนั้นได้อย่างรวดเร็วโดยไม่ต้องเรียกซ้ำจากแหล่งข้อมูลเดิม เช่น การเก็บผลลัพธ์จากฐานข้อมูลที่เรียกบ่อยๆ ไว้ในหน่วยความจำ ทำให้โปรแกรมโหลดข้อมูลได้เร็วขึ้น\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>ประโยชน์ของ Caching\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">1. เพิ่มความเร็ว : ลดการดึงข้อมูลจากแหล่งข้อมูลภายนอก (เช่น ฐานข้อมูล) และเพิ่ม\u003C\u002Fp>\u003Cp class=\"p3\">2. ความเร็วในการเข้าถึงข้อมูล\u003C\u002Fp>\u003Cp class=\"p3\">ลดการใช้ทรัพยากร : ลดจำนวนการดึงข้อมูลซ้ำๆ ช่วยลดการทำงานของระบบ\u003C\u002Fp>\u003Cp class=\"p3\">3.เหมาะสำหรับข้อมูลที่ไม่เปลี่ยนบ่อย : เหมาะกับข้อมูลที่ไม่เปลี่ยนแปลงบ่อย เช่น รายการสินค้า ค่าตั้งต้น เป็นต้น\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>การสร้าง Cache แบบง่ายๆ ด้วย Map ใน Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">ใน&nbsp;Go เราสามารถสร้าง Cache แบบง่ายๆ ได้โดยใช้ map ซึ่งทำหน้าที่เก็บข้อมูลในหน่วยความจำและดึงข้อมูลได้อย่างรวดเร็ว\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างโค้ด Cache แบบง่ายๆ:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้: เราสร้าง Cache struct ที่มี map สำหรับเก็บข้อมูล Set ใช้เก็บข้อมูลใน Cache ส่วน Get ใช้ดึงข้อมูลจาก 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}\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>การตั้งค่าระยะเวลาหมดอายุให้กับ Cache (TTL - Time to Live)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">บางครั้งเราต้องการให้ข้อมูลใน Cache หมดอายุหลังจากเวลาที่กำหนด เพื่อให้แน่ใจว่าข้อมูลที่ใช้งานยังเป็นข้อมูลที่อัปเดตใหม่\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างโค้ดการตั้งค่า TTL ให้กับ Cache:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้:\u003C\u002Fp>\u003Cp class=\"p3\">เราเพิ่ม expiration ให้กับข้อมูลใน Cache โดยใช้ time.Now().Add(ttl).Unix() เพื่อกำหนดเวลาหมดอายุ\u003C\u002Fp>\u003Cp class=\"p3\">เมื่อตรวจสอบข้อมูลใน Cache จะเช็คว่าเวลาหมดอายุผ่านไปหรือยัง หากหมดอายุแล้วจะถือว่าข้อมูลนั้นไม่มีใน Cache\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\ntype CacheItem struct {\r\n    value      string\r\n    expiration int64\r\n}\r\n\r\ntype Cache struct {\r\n    data map[string]CacheItem\r\n}\r\n\r\nfunc (c *Cache) Set(key, value string, ttl time.Duration) {\r\n    expiration := time.Now().Add(ttl).Unix()\r\n    c.data[key] = CacheItem{value: value, expiration: expiration}\r\n}\r\n\r\nfunc (c *Cache) Get(key string) (string, bool) {\r\n    item, found := c.data[key]\r\n    if !found || time.Now().Unix() &gt; item.expiration {\r\n        return \"\", false\r\n    }\r\n    return item.value, true\r\n}\r\n\r\nfunc main() {\r\n    cache := Cache{data: make(map[string]CacheItem)}\r\n    cache.Set(\"session_id\", \"abc123\", 5*time.Second)\r\n\r\n    if value, found := cache.Get(\"session_id\"); found {\r\n        fmt.Println(\"Cached value:\", value)\r\n    } else {\r\n        fmt.Println(\"Value not found in cache or expired\")\r\n    }\r\n\r\n    time.Sleep(6 * time.Second) \u002F\u002F รอให้ Cache หมดอายุ\r\n\r\n    if value, found := cache.Get(\"session_id\"); found {\r\n        fmt.Println(\"Cached value:\", value)\r\n    } else {\r\n        fmt.Println(\"Value not found in cache or expired\")\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>ตัวอย่างการใช้งาน Cache ในโปรแกรมจริง\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">ในโปรแกรมจริง การทำ Caching มักจะใช้กับข้อมูลที่ไม่เปลี่ยนแปลงบ่อย เช่น ข้อมูลผู้ใช้ สินค้า หรือการตั้งค่า ซึ่งทำให้โปรแกรมดึงข้อมูลได้เร็วขึ้นและลดการโหลดข้อมูลซ้ำจากฐานข้อมูล\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>สรุปง่ายๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Cache ช่วยเพิ่มความเร็ว : ใช้เก็บข้อมูลที่เรียกบ่อยเพื่อลดการโหลดซ้ำ\u003C\u002Fp>\u003Cp class=\"p3\">ตั้งค่า TTL: ให้ Cache หมดอายุเมื่อถึงเวลาที่กำหนด เพื่อลดปัญหาข้อมูลเก่า\u003C\u002Fp>\u003Cp class=\"p5\">ใช้\u003Cspan class=\"s1\"> map \u003C\u002Fspan>ใน\u003Cspan class=\"s1\"> Go \u003C\u002Fspan>สำหรับเก็บ\u003Cspan class=\"s1\"> Cache: map \u003C\u002Fspan>ทำให้ดึงข้อมูลได้รวดเร็ว\u003C\u002Fp>","9_11zon_pdridam1g4.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fh5jvtkk3cov51yn\u002F9_11zon_pdridam1g4.webp","2026-03-04 08:34:31.203Z","",{"keywords":15,"locale":108,"school_blog":118},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88,93,98,103],{"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:26:56.612Z","yf74wkqyamfc5qx","โปรแกรมเมอร์","2026-04-10 16:07:36.426Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:31:53.015Z","801w5t09mfaa9hl","ภาษาโปรแกรม","2026-04-10 16:07:46.625Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:32:27.254Z","xl8ixz1jmfnab5f","ฝึกเขียนโปรแกรม","2026-04-10 16:07:54.377Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:32:29.161Z","uewvwdt9cf41o8n","การเขียนโปรแกรมสำหรับมือใหม่","2026-04-10 16:07:54.935Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:34:09.187Z","3zp82zk9hqz13ko","การเขียนโปรแกรม Go","2026-04-10 16:08:08.225Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:32:27.645Z","vfe4f15x4b3afpm","การศึกษาการเขียนโปรแกรม","2026-04-10 16:07:54.538Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:31:30.863Z","oyltq82epf0vqka","การเขียนโปรแกรม","2026-04-10 16:07:41.883Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:27:15.893Z","keubtbdqa4mblx3","การพัฒนาโปรแกรม","2026-04-10 16:07:38.769Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:34:27.419Z","hx7wkkcahseps30","หน่วยความจำ","2026-04-10 16:08:14.896Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:34:28.509Z","cjv7qyxi3nxav82","ข้อมูล","2026-04-10 16:08:15.362Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:34:29.661Z","ttj3ir9k9gvxb6q","TTL","2026-04-10 16:08:15.744Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:32:24.473Z","xquecx7u5svb567","โปรแกรม","2026-04-10 16:07:53.714Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:34:11.160Z","g1ncgq98hse39bh","เพิ่มความเร็ว","2026-04-10 16:08:08.968Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:33:59.518Z","7pqn3y8ffwzzde5","ภาษา Go","2026-04-10 16:08:04.777Z",{"collectionId":17,"collectionName":18,"created":94,"created_by":13,"id":95,"name":96,"updated":97,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":99,"created_by":13,"id":100,"name":101,"updated":102,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":104,"created_by":13,"id":105,"name":106,"updated":107,"updated_by":13},"2026-03-04 08:34:30.952Z","bzngnszne86x3u3","Caching","2026-04-10 16:08:16.304Z",{"code":109,"collectionId":110,"collectionName":111,"created":112,"flag":113,"id":114,"is_default":115,"label":116,"updated":117},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":119,"collectionId":120,"collectionName":121,"expand":122,"id":136,"views":137},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":123},{"blogIds":124,"collectionId":125,"collectionName":126,"created":127,"created_by":13,"id":119,"image":128,"image_alt":13,"image_path":129,"label":130,"name":131,"priority":132,"publish_at":133,"scheduled_at":13,"status":134,"updated":135,"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":131,"th":131},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","u6mc5p13ui5399n",367,"h5jvtkk3cov51yn",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105],"2025-01-27 04:37:31.600Z","เรียนรู้เกี่ยวกับ caching ใน Go เพื่อเพิ่มความเร็วให้โปรแกรม โดยการเก็บข้อมูลในหน่วยความจำ และวิธีการตั้งค่าระยะเวลาหมดอายุ (TTL) สำหรับข้อมูลใน cache","caching-go-speed-up-program","2026-04-25 02:47:32.802Z",{"th":142}]