如何从缓存的后端会话中找到django用户

时间:2015-07-03 01:19:30

标签: django session session-cookies

我偶尔会收到一些错误的电子邮件,但我无法找到该用户。

我可以得到的所有信息都是sessionid and csrf cookie

这些在很多电子邮件中是相同的,所以我认为用户是一个但我怎么能找到它,因为我没有使用数据库会话而是缓存会话引擎。

1 个答案:

答案 0 :(得分:1)

如果您的SESSION_ENGINE设置为:

  • “django.contrib.sessions.backends.cached_db”:结合使用缓存和数据库,您可以从数据库中查找该会话。

  • “django.contrib.sessions.backends.cache”:仅缓存;没有持久性,您必须从缓存中查找该会话,但是如果缓存填满或缓存服务器重新启动,则可以在缓存数据被驱逐之前足够快。在这里,您将不得不在发生错误时查找会话,例如,您将需要处理由该错误引起的异常,此时,您可以使用会话或请求查找用户并将用户存储在日志中。

正如你所说“这些在许多电子邮件中是相同的,所以我认为用户是一个”所以当你收到电子邮件时,会话仍然存在于缓存中!所以在缓存中查找它,例如,如果会话被识别(密钥),其缓存中的“sessionid”尝试使用“sessionid”作为密钥来获取它。

import UIKit
import SpriteKit
import Darwin

class StartGame: SKScene {

var scoreLabel = SKLabelNode(fontNamed: "cholkDuster")
var square = SKSpriteNode(imageNamed: "square")
var circle = SKSpriteNode(imageNamed: "circle")
var rectangle = SKSpriteNode(imageNamed: "rectangle")
var triangle = SKSpriteNode(imageNamed: "triangle")
let bg = SKSpriteNode(imageNamed: "background.png")
var score = 0

override func didMoveToView(view: SKView) {

    //random number for the shapes

    var timecreatShapes = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("creatShapes"), userInfo: nil, repeats: true)

    //background image

    bg.position = CGPoint(x: CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
    bg.size.width = self.frame.size.width
    bg.size.height = self.frame.size.height
    self.addChild(bg)

    self.scoreLabel.text = "0"
    self.scoreLabel.fontSize = 42
    self.scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
    self.addChild(scoreLabel)
    scoreLabel.zPosition = 2

    self.physicsWorld.gravity = CGVectorMake(0, -0.5)

    //declaring a square image

    square.size = CGSize(width: 150, height: 100)
    square.color = SKColor.redColor()
    square.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    square.physicsBody?.dynamic = true
    square.physicsBody?.allowsRotation = false

    //declaring a circle image

    circle.size = CGSize(width: 150, height: 100)
    circle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    circle.physicsBody?.dynamic = true
    circle.physicsBody?.allowsRotation = false

    //declaring a triangle

    triangle.size = CGSize(width: 150, height: 100)
    triangle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    triangle.physicsBody?.dynamic = true
    triangle.physicsBody?.allowsRotation = false

    //declaring rectangle

    rectangle.size = CGSize(width: 150, height: 100)
    rectangle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    rectangle.physicsBody?.dynamic = true
    rectangle.physicsBody?.allowsRotation = false

}

func creatShapes () {

    var x = Int ( arc4random_uniform(4) + 1)
    var a = CGFloat ( arc4random_uniform(1000) + 1)

    switch(x){
    case 1:
        circle.position = CGPoint (x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"circle"))
    case 2:
        square.position = CGPoint(x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"square"))
    case 3:
        rectangle.position = CGPoint(x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"rectangle"))
    case 4:
        triangle.position = CGPoint(x: a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"triangle"))

    default:
        break

    }
    println(x)
    println(a)

}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
       if (self.nodeAtPoint(location) == self.square || self.nodeAtPoint(location) == self.triangle || self.nodeAtPoint(location) == self.circle || self.nodeAtPoint(location) == self.rectangle){

        self.score++

        }
        self.scoreLabel.text = String(self.score)
    }

}

然后得到用户:

from django.core.cache import cache
session = cache.get(sessionid)

这只是一个提示,我不确定它是否会起作用因为我没有与你相同的设置。