Wolfram 语言

云端和网络界面

缓存主要的天气概况网页

如果一个 Delayed 的云对象在需要重新计算之前会有许多人去访问,其输出可以通过 CachePersistence 选项缓存一段时间.

定义一个函数计算里约热内卢过去一周的温度记录.

显示完整的 Wolfram 语言输入
In[1]:=
Click for copyable input
rioTemperatureHistory[] := With[{temps = AirTemperatureData[ GeoPosition[ Entity["City", {"RioDeJaneiro", "RioDeJaneiro", "Brazil"}]], {Now - Quantity[1, "Weeks"], Now}]}, labelcolor = GrayLevel[0.4]; tempcolor = GrayLevel[0.4]; grid = Grid[{{Style["Current", labelcolor], Row[{Style[ QuantityForm[Round@Last[temps["Values"]], "Abbreviation"], tempcolor], " "}]}, {Style["High", labelcolor], Row[{Style[ QuantityForm[Round@Max[temps["Values"]], "Abbreviation"], tempcolor], " "}]}, {Style["Low", labelcolor], Row[{Style[ QuantityForm[Round@Min[temps["Values"]], "Abbreviation"], tempcolor], " "}]}}, Alignment -> {{Right, Left}}]; table = Style[Column[{Row[{"Past Week "}, Alignment -> {Bottom, Right} ], grid}, Alignment -> Right], 16, RGBColor[ 0.9098049191388243, 0.3176450101594796, 0.24313742164725222`]]; DateListPlot[temps, PlotRange -> {55, 100}, AxesLabel -> Style["\[Degree]F", 20, Black], TicksStyle -> Directive[Black, 14], Ticks -> {None, Automatic}, PlotStyle -> Directive[AbsoluteThickness[2], Black], Filling -> Bottom, FillingStyle -> Directive[RGBColor[ 0.9098049191388243, 0.3176450101594796, 0.24313742164725222`], Opacity[1]], Epilog -> Inset[table, Scaled[{0.95, 0.9}], {Right, Top}], PlotTheme -> {"Wide", "Presentation"}, ImageSize -> 550]] rioTemperatureHistory[]
Out[1]=

部署该函数使之成为一个应用程序,且会将其结果缓存 5 分钟. 这样,服务器无需每次都重新计算出结果,成千上万的用户就可以访问该应用程序,并得到基本上实时的信息. 其结果是更好的响应时间和更低的服务器负载.

In[2]:=
Click for copyable input
obj = CloudDeploy[ Delayed[rioTemperatureHistory[], "PNG", CachePersistence -> Quantity[5, "Minutes"]]]
Out[2]=

相关范例

de en es fr ja ko pt-br ru