Skip to content

Instantly share code, notes, and snippets.

@honewatson
Created October 7, 2016 22:20
Show Gist options
  • Select an option

  • Save honewatson/3dddff2d28d98e94a3ee7487ba970a3d to your computer and use it in GitHub Desktop.

Select an option

Save honewatson/3dddff2d28d98e94a3ee7487ba970a3d to your computer and use it in GitHub Desktop.
Moonscript Lapis cached widget examle
lapis = require "lapis"
db = require "lapis.db"
html = require "lapis.html"
queries = require "queries"
config = require("lapis.config").get!
widgets = require "views.widgets"
import get_redis from require "lapis.redis"
import render_html from require "lapis.html"
import notNil from require "utils"
query = (q, blog_id) ->
return string.gsub q, '_#_', "_#{blog_id}_"
get_set_redis = (key, query, view) =>
content = @redis\get key
unless notNil content
@query = query
@view = view
content = @view @query!
@redis\set key, content
return content
main_query = () =>
@main_query_results = db.query(query(queries.main, @blog_id), 0, 40)
main_posts = () =>
render_html ->
div class: "body", ->
for post in *@main_query_results
div ->
h2 post.post_title
h3 post.post_excerpt
post.post_read_more
class App extends lapis.Application
@before_filter =>
@blog_id = config.blog.default
blog = db.query queries.blog, @blog_id
@blog = {row.blog_option_name, row.blog_option_title for i, row in ipairs blog}
@redis = get_redis!
@get_set_redis = get_set_redis
[index: "/"]: =>
content = @get_set_redis @req.parsed_url.path, main_query, main_posts
{
layout: false,
content
}
@honewatson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment