Web前端架构师

osc_35890167 2021-04-08 04:41:52
redis


download:Web前端架构师

超大复杂项目+高端技术+大厂规范+全局架构思维
系统培养大厂P7技术专家/中小厂前端Leader
6~8个月,让3年+前端完成质的飞跃
func AsyncAdd(run func() error) {
//TODO: 扔進異步協程池
go run()
}
func GetInstance(ctx context.Context,id uint64) (string, error) {
data,err := GetFromRedis(ctx,id)
if err != nil && err != redis.Nil{
return "", err
}
// 沒有找到數據
if err == redis.Nil {
data,err = GetFromDB(ctx,id)
if err != nil{
return "", err
}
AsyncAdd(func() error{
return UpdateCache(ctx,id,data)
})
}
return data,nil
}
func GetFromRedis(ctx context.Context,id uint64) (string,error) {
// TODO: 從redis獲取信息
return "",nil
}
func GetFromDB(ctx context.Context,id uint64) (string,error) {
// TODO: 從DB中獲取信息
return "",nil
}
func UpdateCache(ctx context.Context,id interface{},data string) error {
// TODO:更新緩存信息
return nil
}
func main() {
ctx,cancel := context.WithTimeout(context.Background(), 3 * time.Second)
defer cancel()
_,err := GetInstance(ctx,2021)
if err != nil{
return
}
}
剖析
我們先简單剖析一下,這一段代码要幹什麼?其實很简單,我們想要獲取一段信息,首先會從緩存中獲取,假如緩存中獲取不到,我們就從DB中獲取,從DB中獲取到信息後,在協程池中放入更新緩存的辦法,異步去更新緩存。整個設計是不是很圆滿,但是在實践工作中,異步更新緩存就沒有勝利過?

招致失敗的缘由就在這一段代码:

AsyncAdd(func() error{
return UpdateCache(ctx,id,data)
})
复制代码
錯誤的缘由只要一個,就是這個ctx,假如改成這樣,就啥事沒有了。

AsyncAdd(func() error{
ctxAsync,cancel := context.WithTimeout(context.Background(),3 * time.Second)
defer cancel()
return UpdateCache(ctxAsync,id,data)
})

版权声明
本文为[osc_35890167]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/5067864/blog/5011618

  1. The difference between pseudo class and pseudo class elements
  2. [CSS effect] simple drop-down menu
  3. [Vue] value transfer by parent-child component
  4. 【css】设置table表格边框样式
  5. 【css】修改input,textarea中的placeholder样式
  6. vue-router的两种模式(hash和history)及区别
  7. CSS3的滤镜filter属性
  8. [CSS] set table border style
  9. [CSS] modify the placeholder style in input and textarea
  10. Two modes of Vue router (hash and History) and their differences
  11. Filter property of CSS3
  12. 全局安装gulp 报错问题解决
  13. Solution of error report in global installation of gulp
  14. 18个好用的自定义react hook
  15. 你应该知道的常用服务器HTTP状态码?
  16. 18 user defined react hooks
  17. What HTTP status codes should you know about common servers?
  18. 手把手教你打造属于自己团队的前端小报系统
  19. Hand in hand to teach you to build your own front-end tabloid system
  20. In 2021, enterprise SEO actual operation, how to less update, batch ranking regional words?
  21. vue cli4.0 快速搭建项目详解
  22. Vue cli4.0 quick build project
  23. vue-cli脚手架安装
  24. Installation of Vue cli scaffold
  25. [JS knowledge] method of getting elements from DOM
  26. 【jQuery效果】文字滚动
  27. [jQuery effect] text scrolling
  28. [front end] live broadcast of user experience optimization series, real-life sharing of front-line celebrities
  29. React native introduces third party Android SDK
  30. Using html2canvas to generate shared images, CDN images do not show the problem
  31. Using hooks to write react components
  32. Explain the module hot replacement function of webpack in detail
  33. An incomplete guide to writing a simple native wechat applet
  34. How JavaScript calculates 1 + 1 - Part 1 creates a source string
  35. Jsonp method to solve cross domain problems
  36. Canvas animation demo (from zero to one)
  37. El dialog of elementui component encapsulation
  38. Transition group of Vue source code
  39. When encountering bracket validity, next larger element, specific minimum value, try stack
  40. Vue3 virtual DOM
  41. Scheme and implementation of front end page watermarking
  42. Why is 0.1 + 0.2 not equal to 0.3?
  43. JS arrow function this points to related practice
  44. CSS text decoration & text emphasis
  45. 2021必修 首门CSS架构系统精讲 理论+实战玩转蘑菇街
  46. 大前端
  47. The first compulsory CSS architecture system in 2021
  48. Big front end
  49. 你喜欢才是最好的,前端工程师常用的8个工具
  50. What you like is the best. There are 8 common tools used by front-end engineers
  51. Spring Boot + Vue3 前后端分离 实战wiki知识库系统
  52. Spring boot + vue3 front end and back end separation practical wiki knowledge base system
  53. CSS高级技巧总结
  54. CSS advanced skills summary
  55. HTML5教程 - HTML5 事件
  56. HTML5 tutorial - HTML5 events
  57. Vue和微信小程序的区别
  58. The difference between Vue and wechat applet
  59. HTML5和CSS3提高
  60. Improvement of HTML5 and CSS3